How to make object follow y-axis of another object.

I am making a 2D game where you can move a square around using the WASD keys. I have an enemy that I want to make the y-pos of = to the square’s y-pos at a certain speed. Currently all I have is
transform.position = new Vector3 (transform.position.x, Character.transform.position.y);
The enemy moves just as fast as the character, I want to slow it down a bit so the character has time to get past him. Thanks!

Thank @BoredMormon ; if the link helped you Answer this, please tick the "Accept" mark and upvote his Answer if you want to/can.

1 Answer

1

Instead of making the position the same as character position, why don’t you use Vector3.lerp , or Vector3.Move Towards.

transform.position = Vector3.MoveTowards(transform.position, target.position, step);

Directly from Unity’s documentation!

This should help you get on the right track!

I'm not sure if you saw, but I am making a 2D game, so Vector3 items won't work. I tried doing something like this. transform.position = Vector2.MoveTowards (transform.position, Vector2 (transform.position.x, CharacterTransform.position.y, Time.deltaTime * 3));

It is working perfectly! Thank you so much. I just simply put the parenthesis in the wrong place.

Thank you! Been searching around for days, toying with complicated code that spans multiple lines and trying to do vector math. Lo and behold, you present just one line and it's the solution! It actually makes sense and it makes my object follow the target on whatever axis I specify. Thank you again!