#Keep the same speed when following the player

1 messages · Page 1 of 1 (latest)

tepid valve
#

Hi, I am making an enemy, it's a simple AI, it's going to only wander around until detects the player, but, for some reason, when it does detect the player, the speed of the enemy is decreased when it's closer to the player. Is there a way that I keep the same speed? Also, if possible, a way to flip the sprite to the player side

#

I know that is something related to the player position been decreasing when the enemy approuches, if I was woking in both axes I need to normalize, but since I am woking only with the X axis, I have no idea what to do

#

I want something like this, keeping the same speed besides the distance, but only in the X axis

worn tulip
#

To give you a simple example on what Normalize is.
Imagine the player is at coordinate x 10. The enemy is at x 20.

x 10 - x 20 = speed is -10.
Then enemy moves closer so now it's at 10.
x 10 - x 10 = 0. The speed is lower now.

What normalize does is it gets rid of the magnitude, the distance, and leaves only the direction.
That way it's always 1, or -1 for the direction.

If you only need X you could just check if the player is to the left or right.
if (player.position.x < enemy.position.x) means player is to the left.
And of course > would be to the right.

You can just set it to "-1 * speed" for left or "1 * speed" for right if you want consistent speed.

#

...oh, might need to use global_position instead of position ... I was more focused on the math of it, sorry. Haha

tepid valve
worn tulip
#

Yeah if you want consistent speed you don't want the distance, you jsut want the direction.

#

Though you might need to check distance if you want the follow to stop. Otherwise it will walk to the center of the player.

tepid valve
#

something like this (at least it worked)

tepid valve
#

it's just player.global_position.x - global_position.x <= [some value]

#

right?

worn tulip
#

Hm almost. Keep in mind that being at left means negative numbers.

I think it'd be abs(player.global_position.x - global_position.x) <= [some value]

Abs makes it so that negatives turn into positives. (and it doesn't do anything to positive)

tepid valve
tepid valve
worn tulip
#

Place the abs around abs(player.global_position.x - global_position.x) the <= 40 should be outside it

tepid valve
#

oh

#

thank you

#

it worked!

wicked trout
#

If you want to check the distance is less than 40, take out the sign and use that value abs(value) <= 40:
Then just add sign back for the actual movement

#

sign makes it -1 or 1

tepid valve
#

I am not home now, so I can't test it :(

#

When I get home I gonna try though

wicked trout
#

I don't know what your code is like

#

Does this movement functionality run when inside and outside the area?

#

or just inside?

#

And what do you want it to do outside of the area

#

Ah, your talking about the >= 40?

tepid valve
#

Basically this

#

But like, if I put the position like that to move with the player position if it's not inside the area, then it's going to be 0

#

I can show more when I get home, I think I will get in one hour or something like that

wicked trout
#

Sorry, do you want it to be 0 outside of the area or you don't?

wicked trout
tepid valve
#

But when the player is in the area2D, moves to the player direction and when gets to close, stops

wicked trout
#

Ah okay

wicked trout
#

When your next Working on it :)

wicked trout