I am trying to make an enemy follow my player without changing its rotation
this is the code im using
void Update () {
//move towards the player
if (Vector3.Distance(transform.position,target.position)< aggroRange){
transform.position = Vector3.MoveTowards(transform.position, target.position, speed*Time.deltaTime);
}
}
but the problem with this is it for some reason doesnt follow me all the way and has some sort of offset.
How do i make the enemy follow me all the way to the player.
If speed*time.deltaTime becomes a negative number then it will move away.
If you have rigidbody’s attached and colliders then it may collide and stop never reaching/overlapping the other 3d object.
Put a debug statement in your code and watch it:
void Update () {
//move towards the player
if (Vector3.Distance(transform.position,target.position)< aggroRange){
float newSpeed = speed * Time.deltaTime;
Debug.Log("My Speed: " + newSpeed.ToString());
transform.position = Vector3.MoveTowards(transform.position, target.position, newSpeed);
}
}
You’re asking for 2 different things or I’m not understanding your question.
MoveTowards takes a point and a target point, a returns a new point between those 2, so it always gives you a point closer to the target. It doesn’t change the rotation of the object, and the code you provided doesn’t change the rotation either. If your enemy is being rotated and you don’t want that behaviour, the problem is in another script.
About the offset you’ll have to be more specific, it’s important to know what the transform.position actually is, maybe your player model has it’s origin in a corner instead of the center, so the enemy moves to that corner.