#Keep the same speed when following the player
1 messages · Page 1 of 1 (latest)
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
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
oh, so I don't need to calculate the speed, just check where the player is
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.
something like this (at least it worked)
yeah I need it to stop
it's just player.global_position.x - global_position.x <= [some value]
right?
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)
just need now to fix this... "interesting" behavior
hmm, okay
gonna try
hmm, i don't think this is normal
Place the abs around abs(player.global_position.x - global_position.x) the <= 40 should be outside it
If you don't want an if statement, I think position.x += sign(player.global_position.x - abs(global_position.x)) * speed
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
But if the player is not in the area, the speed wouldn't be 0?
I am not home now, so I can't test it :(
When I get home I gonna try though
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?
Outside
Like, the enemy walks right and left, and when the player enters in the area2D, change the direction to go to the player
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
Sorry, do you want it to be 0 outside of the area or you don't?
This is for only moving when inside/outside the area depending on the direction of the alligator <><>><><
No, I want to the enemy walks even when the player it's not in the area2D
But when the player is in the area2D, moves to the player direction and when gets to close, stops
Ah okay
Swap the > to a < in this, and only do movement if this condition is true
When your next Working on it :)
the value of this will be the same as the direction, just with less if statements and clutter