- Home /
Vector2.Movetowards setting z axis to 0
I have an enemy gameobject that I want to move towards my player at a constant consistent speed. I have it's z axis set to 100 but as soon as it follows the player it gets reset to zero when it starts following the player. I have commented out the follow line of code and determined that it is what is messing up my z axis. The line in question is
transform.position = Vector2.MoveTowards(transform.position, player.transform.position, speed * Time.deltaTime);
As far as the movement goes it works exactly as I would want it to. I am having the classic case of being able to see the object in scene but not game view. I have found a couple different forum posts with my exact issue but neither of them ended up getting resolution. I initially used vector3.movetowards and the z axis wasn't an issue but the movement wasn't what I wanted. The further the player got from the enemy the more the enemy sped up in a sort of close the gap kind of way. Is there a way to lock the z-axis and use my current line of code which produces the behavior I want or a way to use Vector3.movetowards in a way that keeps the enemy at a constant speed?
Answer by Caeser_21 · Apr 23 at 01:27 AM
Maybe you can try this :
Vector3 PlayerPos = new Vector3(player.transform.position.x, player.transform.position.y, 100f);
Vector3.MoveTowards(transform.position, PlayerPos, speed * Time.deltaTime);
Your answer
Follow this Question
Related Questions
2D Physics and Jump-Through Platforms 2 Answers
Get Width of 2d Game Object 1 Answer
How can I stop my object pool from firing? 0 Answers