- Home /
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!
Answer by ahaykal · Jul 20, 2016 at 06:55 AM
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.$$anonymous$$oveTowards (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!
Your answer
Follow this Question
Related Questions
Spawn object at a random predeterminated(transform) position 1 Answer
Adding a value to transform.position.y runs very untrusted 0 Answers
2D RPG boomerang instantiation help. 2 Answers
Unity3D - Playback object array of position (with dynamic velocity) 0 Answers
How to Position the object perfectly with the Screen Bounds 0 Answers