Making a character move toward another character?

This might be a silly question but I haven’t been able to find anything about it yet, so here goes:

Is there a way to make one character move toward another one? Like if you had zombies chasing the player character and they’re always moving in a straight line toward the player.

I use GameSalad a lot and there’s a “Move To” function that’s super useful. Is there a similar thing in Hopscotch? Or some sort of workaround that would accomplish the same thing?

10 Likes

Welcome to the forum. There's several different ways to accomplish the movement.

1st option

You could have a repeat forever loop in the zombie:
Set Position to x: Zombie_X_position + (Hero_X_position - Zombie_X_position)/Abs(Hero_X_position - Zombie_X_position) * Step_Size
repeat the formula for the y:

Make the Step_Size however far you want it to move with each loop.

Note that this has a couple draw backs.
1. The step_size controls the apparent speed. You can't use Set Speed
2. The formula results in a faster apparent speed (1.414 times faster) as the direction approaches 45 deg (in any of the 4 quadrants)

2nd option
Make When 7=7 (which is another way of making a repeat forever) in the zombie with:
move forward (100)

Make another When 7=7 with:
Theta = arctan((Hero_Y_position - Zombie_Y_position)/(Hero_X_position - Zombie_X_position))
If Zombie_X_position > Hero_X_position
Increase theta by 180
End if
Set Angle (theta)

Where Theta is a variable that you'd make.
In a When Game Starts for the Zombie, set the speed that you want (or change it at any time)

Note: this also has a draw back that the character doesn't stay "right side up" if that's what you had wanted.

Good luck & have fun! Tag me with questions. There's still more options if the above don't meet the specific needs of a given project.

P.S. There's no such thing as a dum.b question. Never worry about that! Asking questions is the process by which we all individually learn. :grinning:

15 Likes

Interesting.

This format is rather nice.

I am gonna try it sometime!

1 Like

Welcome to the forum by the way!

@ThinBuffalo has a great :+1: understanding about how to do this.

1 Like

Great, thanks for the advice. Unless I'm mistaken, there aren't any built-in functions for abs, atan, etc, yes? So if I want the absolute value, I'll need to sort of make my own absolute value function by squaring and then unsquaring the number? Same for other functions beyond the built-in sin/cos/squareroot?

1 Like

There are functions for ABS and Arctan.
Do you have the latest update?

1 Like

I must have an older version. Ok, having built-in absolute/arctan/etc will make this much easier. I'll upgrade ASAP. Thanks for the tip!

2 Likes

Btw welcome! Tag me anytime you want help and I'll try as best as I can! Just type "@KVJ"!

I know this is unrelated, but welcome to the forum!

What's GameSalad? It's possible, but it's really hard xD

Typing block Hopscotch looks really weird, but text-based Hopscotch would be nice. Why can't they have build-in Swift support?

Thank you for this great code sample @ThinBuffalo ! :grinning:

I’m testing code for a game (before adding graphics) and just tried this code sample you shared, and although it mostly works (which is awesome btw!), I’m having a problem and I’d love your help! I used the first approach you shared (as I do not want the zombie to rotate, which occurs in your second approach).

The zombie (in this case a robot) is activated by a switch on the floor (a black flower icon in this demo, which opens a door). This makes the robot move, but this robot reappears in the bottom left corner (with the code you shared) and not in its original position. The robot only seems to move up and to the right, but not down or to the left.

Click here for a sample video of the code in action.

1) How can I get the robot to also chase the hero down or to the left?

2) Also how do I set the initial position the robot will move from, while still being able to be activated from its resting position (activated by a variable called ‘Switch’ being equal to the value of ‘1’ when the flower is bumped)?

FYI I coded the robot to bounce off the walls. The goal is for the monsters to be activated once rooms are opened and collide with walls in an effort to chase the hero (and once in contact with the hero, applying damage to the hero).

I’ve got it all working in concept, except for fixing the robot so that it also moves down and to the left, from its original standby position, and only once activated by the floor switch. I’m stumped!

Thanks in advance for your help!

iShaman

P.S. Here is an example image of the code I have setup for question #2, for the switch activating the robot, and trying to put it in the correct position before it moves.

2 Likes

Hello @ishaman and welcome to the Hopscotch forum.

Without seeing your code, I can’t say why Robo doesn’t chase down or to the left. It should.

For question #2 and looking at your code screenshot: Notice how you’re setting the position and then entering into a Repeat Forever loop. That means the Position will only ever be set once and the code in the loop will always thereafter be executing. In other words, Robo will always be chasing Miss Chief.

I’d like to suggest a 3rd option that I didn’t cover in the original response to the question. I think this should solve both of your problems.

I’m quite sure how you’re handling the visibility or position of Robo, but in this example the code whatever needs to happen before Robo starts chasing Miss Chief can be accomplished in the When Bumps rule before setting the Switch variable.

Then the last 2 rules for When Switch = 1 only execute after the Flower is bumped. The 1st rule calculates the angle from Robo to Miss Chief. The 2nd rules moves Robo towards Miss Cheif.

The full equations are

If MCx > Rx 
    Set 'Chase_Angle' to arctan( (MCy - Ry)/(MCx - Rx) )
Else
    Set 'Chase_Angle' to arctan( (MCy - Ry)/(MCx - Rx) ) + 180
End If
Set Position to x (Speed * cos(Angle)) + Xpos) y (Speed * sin(Angle)) + Ypos)

Note: The Speed means the same as Step_Size previously. You could keep the extra variable if you want to. I just didn’t bother when creating the example.

2 Likes

Hi @ThinBuffalo !

Thank you so much for your quick response!!! You rock :sunglasses:

Your new version of the code works amazing. It’s much more accurate than the previous versions, but there’s still one issue. The robot still doesn’t move to the left (although it does move up, down and to the right).

I realized that in my last post I was wrong when saying the robot did not move down before (but only right and up). In my previous (first) video it shows the robot chasing the hero to the right, up AND down. Just no left movement, so the no left movement is a persisting issue.

I removed all of the other objects and code that were in all characters and objects to be sure nothing was causing this in my other code, but it still reacts the same way with no left movement for the robot when chasing the hero.

Here is an updated (second) video clip.

When you tested this on your end, did the robot chase when the hero moves to the left of the robot?

Thanks again for your amazing help!

iShaman

@ishaman
Yes, when I tested the code Robo chased left, right, up, and down.

If you wanted to temporarily publish the project and post a link, I’d be happy to have a look and see why Robo doesn’t move to the left

2 Likes

Hi and welcome to the forum! Here is a topic that may help you:


And feel free to tag me @Awesome_E for anything.

If you need help with forum-related issues, tag @Leaders or @Ana, and if you need help with code, tag @CodeHelp

Thanks @ThinBuffalo !

I’ve been offline for the past month having systems issues. I will be showing my game to a coding club at my school this Thursday at lunch, and would love to still fix the issue I have (robot wont move left).

I have published the game temporarily at this link: https://c.gethopscotch.com/p/102i1638ks

I am waiting for Hopscotch to publish this test. Please let me know what I am doing wrong!

Should I take screenshots of my code in case Hopscotch doesn’t publish it in time for you to review before my Thursday presentation?

Also how would I add extra enemies (extra robots)? Would they just use the same code, or would I need to do something special?

Thanks again for your help! You have really inspired me with this game.

iShaman

2 Likes

For the robot not moving correctly, when you set the chase angle, you have the +180 in the wrong place. It should be outside of the arctan, not inside of it.

For the question about reusing the code, if you want the new robots to chase the same character, then you can’t with it how it is.
Making it so that you can is easy though. All you have to do is replace (in the code you want to reuse) all references to the robo object (for example, in when bumps events or for object variables.) with self. This will refer to the object that is doing the code, not just robo. Then create a custom ability with the code you just changed inside of it.

Make sure that you don’t put anything which will put the two (or more) robots in the same place anywhere, since then it’ll only look like one.

4 Likes

I wanted to let you know that this WORKED! :grinning:

Thank you so much for your help fixing the code!!

I haven’t tried any of the other suggestions, but the +180 fix has already made a big difference.

You rock!

2 Likes