Making a character move toward another character?


#1

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?


#2

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:


#3

Interesting.

This format is rather nice.

I am gonna try it sometime!


#4

Welcome to the forum by the way!

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


#5

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?


#6

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


#7

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!


#8

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


#9

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

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


#10

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