- Home /
Rotation effecting movement
I'm making a basic 2D top-down shooter and right now I'm spawning a bunch of simple cube gameobjects which are chasing the player with the following logic:
Vector3 pos = player.transform.position - gameObject.transform.position;
pos.Normalize();
gameObject.transform.Translate(pos * enemySpeed);
They chase fine, but to make them feel a bit more 'alive' I wanted to make them spin in circles while chasing the player. I have been able to get them to rotate using:
gameObject.transform.Rotate(0, 0, 90 * Time.deltaTime);
The problem is this also effected the movement. How would I go about getting these objects to rotate in a circle without effecting the chase movement?
Answer by aldonaletto · Apr 25, 2012 at 10:57 PM
Translate by default uses local space - thus when the cube rotates the displacement passed to Translate is rotated too. You should specify world space in Translate:
transform.Translate(pos * enemySpeed, Space.World);
NOTE: you don't need to write gameObject.transform.Translate - transform.Translate works fine and is faster. The same apply to gameObject.transform.position: it could be only transform.position
Thanks, worked like a charm. Also for the additional info!