- Home /
Vector2.MoveTowards and Rigidbody2D Velocity
I am scripting an AI Enemy that is supposed to move toward and retreat from the player based on distance ranges. This is working fine, however when I expand the Info on the Rigidbody of the AI, the X Velocity is stuck at 0.
This is an issue because I am trying to flip the AI Enemy when its X velocity is in the negative (e.g. movement > 0.0f)
Here is my script for how the AI moves towards the player, and the script for flipping the sprite.
//Simple AI Behaviors
if (Vector2.Distance(transform.position, player.position) > stoppingDistance)
{
transform.position = Vector2.MoveTowards(transform.position, player.position, movementSpeed * Time.deltaTime);
}
else if (Vector2.Distance(transform.position, player.position) < stoppingDistance && Vector2.Distance(transform.position, player.position) > retreatDistance)
{
transform.position = this.transform.position;
}
else if (Vector2.Distance(transform.position, player.position) < retreatDistance)
{
transform.position = Vector2.MoveTowards(transform.position, player.position, -movementSpeed * Time.deltaTime);
}
movement = GetComponent<Rigidbody2D>().velocity.x * movementSpeed;
//Flip Player Sprite
if (movement > 0.0f)
{
transform.rotation = Quaternion.Euler(0, 180, 0);
}
else if (movement < 0.0f)
{
transform.rotation = Quaternion.identity;
Debug.Log("SpiritAnimal");
}
My assumption is that Vector2.MoveTowards is not sending X Velocity information to the Rigidbody of the AI Enemy... how could I re-write my pursuit code to utilize Rigidbody Velocity?
Thank you.
Answer by bobisgod234 · Apr 09, 2018 at 05:28 AM
Setting transform.position is like teleporting the object around, it will look like smooth movement, but to the physics engine, there is no velocity.
Try this, but put it at the top of your script, before you assign to transform.position:
movement = player.position.x - transform.position.x;
Also, line 8 doesn't actually do anything, you just assign a value to itself, since transform and this.transform are the same thing.
That got it working, thank you!
I notice that the velocity is still not being reported, though I suppose that is due to the $$anonymous$$oveForward not reporting velocity like you mentioned. I wonder if there is a way to do this and still apply velocity...
One odd thing that is going on is my Enemy AI gets super jittery as it moves, but when it hits the ceiling of the level (the AI flys like a flappy bird), it smoothens itself out perfectly, only to repeat the cycle over and over again. Any thoughts? Otherwise, thank you for solving my original issue!
Answer by ItWillBeEpic · Apr 09, 2018 at 04:58 AM
Have you tried flipping enemy entirely instead of just flipping its sprite?
Yes, actually that is what I am doing. Apologize for my mistyping!
Answer by nkcowmaster · Apr 10, 2018 at 03:26 AM
@Bobisgod234 had the answer I was looking for, thank you all for your help!
Bob, if you can toss a reply in the comment section I can Accept your answer as the correct one. I don't see the option for your original reply.
I converted my comment to an answer, so you can accept it.