- Home /
Follow and Rotate an object
Hello,
I have 2 objects in a scene. 1 - enemy that spawns in the middle of a circle and 2 - the player.
Now my problem is, I want the player to rotate in a 360degree angle and follow the enemy.
Here's a sample image.
The ENEMY moves randomly at start and the PLAYER is just moving in a CIRCULAR direction FOLLOWING the ENEMY position. How do I rotate the PLAYER and follow the direction of the ENEMY?
Thanks in advance!
Answer by Lukas-Barbarossa · Jul 24, 2012 at 11:54 AM
I think you might use Transform.RotateAround and pass it the transform.position of the enemy in the Update();
function RotateAround (enemy.transform.position:Vector3, axis:Vector3, angle:float) : void
You might want to combine it with Transform.LookAt() if you need the player to always be facing the enemy.
Hope that helps.
Thanks, I just need to lock it's x,z rotation since I need to rotate only it's Y.
You may just be able to do that by using the second argument, as this is the axis to rotate on. Something along the lines of
function RotateAround (enemy.transform.position:Vector3, Vector3.up, angle:float) : void
I've never done it though. I'd be interested t hear whether it worked.
It's a working progress. I can't lock the player to rotate only on it's normal orbit. It folows the ball and rotates arount it. It is rotating but the player rotates around the ball while the ball is moving.