- Home /
Help using Vector2.MoveTowards
I want my enemy to move towards the player and for now I just dropped this in FixedUpdate() to test it out.
The problem is it changes Z axis for some reason and is far away from visible.
Here is the code:
transform.position = Vector2.MoveTowards(transform.position, Player.transform.position, moveSpeed * Time.deltaTime);
The Player and the Enemy are at the same world Z axis(-1940.75) so I don't understand why it's changing so much or at all. It should just be changing X and Y, especially because it's a vector2.
The Player is a child object where as the Enemy is not a child of anything. I do have the correct GameObject referenced.
Answer by GrayLightGames · Nov 05, 2019 at 05:16 AM
Hi @knobblez, setting transform.position (a Vector3) equal to a Vector2 will default the z component to 0. So if your player is not already at z = 0, it will immediately move to z = 0 from this code.
Is there a reason not to use Vector3.MoveTowards? That would be easiest, but you could always do something like this if you truly want to disregard z for this type of movement:
Vector2 tempVector2 = Vector2.MoveTowards(transform.position, Player.transform.position, moveSpeed * Time.deltaTime);
transform.position = new Vector3(tempVector2.x, tempVector2.y, transform.position.z);
Hope that helps!
That did the trick! I want to keep the Enemy a couple points below the Z axis of the Player so that the Player is always on top. Thank you for your help!
Glad I could help, good luck with your project!
Answer by naviln · Nov 05, 2019 at 12:14 AM
hmm. I think transform.position is a Vector3. Maybe try casting that to a Vector2 first, before passing it to Vector2.MoveTowards? Here is an example:
Vector2 v2 = new Vector2(transform.position.x, transform.position.y);
Vector2 v2Player = new Vector2(Player.transform.position.x, Player.transform.position.y);
transform.position = Vector2.MoveTowards(v2, v2Player, moveSpeed * Time.deltaTime);
That didn't work - same result. I appreciate the reply though
Sorry mate.
I am using this tweening library (it has a free version): http://dotween.demigiant.com/documentation.php
It has a Do$$anonymous$$oveX and Do$$anonymous$$oveY, where you can explicitly move individual x and y components. It seems to work well for me - see if that is an option for your project?
DO$$anonymous$$oveX/DO$$anonymous$$oveY(float to, float duration, bool snapping) $$anonymous$$oves the target's position to the given value, tweening only the chosen axis (note that in case of rigidbodies, the other axes will still be "locked" by the tween).
Your answer
Follow this Question
Related Questions
Move player between 3 given points only by touching on the left/right side of the screen 1 Answer
How can I move an object to a (0,0) and scale it to (0,0) at the same time? 1 Answer
Using time.deltatime to move from coordinate to coordinate is too fast? 1 Answer
How to get a proper ramming effect? 1 Answer
How do I get one object to move to (the closet) another object? 2 Answers