How move and rotate at the same time?

I have a gameObject that rotates around its own y-axis with:

transform.Rotate(Vector3.up, spinSpeed * Time.deltaTime);

This works, but now I want it to move left as well. When I add another translate, suddenly the Y axis for the rotation is displaced and the object starts rotating in a very wide circle!

transform.Rotate(Vector3.up, spinSpeed * Time.deltaTime);
transform.Translate(Vector3.left * 10 * Time.deltaTime);

How can I keep the object rotating around its own axis while moving left?

You’re translating the object by its own local Vector3.left, but as it rotates, what left means changes. The result is the object moving around the circumference of a circle while it spins.

To stop this, you could specify the space you want your rotation and translations to happen:

transform.Rotate(Vector3.up, spinSpeed * Time.deltaTime, Space.Self);
transform.Translate(Vector3.left * 10 * Time.deltaTime, Space.World);

Now the rotation is local, but the translation is relative to the world’s Vector3.Left.

2 Likes