Rotate an object (in 2D) while keeping it moving in a straight line

Hi,

I am creating my first game in Unity - a side-scrolling space shooter. I have some asteroids sprites that I would like to rotate as they move along the screen. At first I tried the animations system, but every time it gets back to 360 degrees, it “jerks” back to 0 degrees and starts again. I’m not sure if it’s possible to keep it going round and round that way… so I decided to try something in code instead. I wrote the following generic script to keep rotating any object indefinitely:

public class RotateIndefinitely : MonoBehaviour
{
    public Vector3 Degrees;

    public void Update()
    {
        float x = Degrees.x;
        float y = Degrees.y;
        float z = Degrees.z;

        if (x != 0f)
        {
            x *= Time.deltaTime;
        }

        if (y != 0f)
        {
            y *= Time.deltaTime;
        }

        if (z != 0f)
        {
            z *= Time.deltaTime;
        }

        transform.Rotate(x, y, z);
    }
}

It sort of works in that it does rotate the asteroids (I only set the Z axis by the way), but… they also keep moving up and down and I think this is probably because when the object is rotated, then the “forward” direction is changed, if that makes sense. How can I keep the asteroids rotating, but going in a straight line? I figure I need to somehow store the original position or something and keep resetting it to that… I’m not sure.

Any help would be much appreciated.

You probably don’t need this answer anymore but I just had the same problem.

I had this 2 lines of codes:

          //object will move down on its own coordinate system * object speed * seconds

transform.Translate (Vector2.down * _speed * Time.deltaTime);

          //object will rotate on its own coordinate system 100 degrees per second

transform.Rotate(0,0,100*Time.deltaTime);

The problem with that is that when the object rotates on its z-axis, his coordinate system rotates as well, making the “Vector2.down” thing not go down in the game-world, and the object just starts doing like a circling movement.

The only thing I had to add to fix this issue was “Space.World”. And now its like this:

          //object will move down on the world coordinate system * object speed * seconds

transform.Translate (Vector2.down * _speed * Time.deltaTime, Space.World);

Hope I could help anyone.

Make one object that is continuous rotating through your script which you have mentioned.
Make Parent of that object that is for moving purpose.
Simple… :slight_smile: @vnmatt

OK, I did finally figure out how to make it work with the animation system…

Firstly, I’m not sure why it was jerking back and forth the first time I tried that, but it’s not doing it anymore… probably I used a wrong Z value on the last key frame or something.

Secondly, I was still getting the object moving up and down as the direction was changing, so I decided to create a child object for the Sprite Renderer and apply the animation to that child object - not the main object. So that keeps it going in a straight line!

I will be deleting that script and relying on the animation only.

The only small thing left to fix is to figure out why at the end of the animation, there is a very small pause… probably no more than a half second or probably less, but enough to be annoying to the eye… since the asteroid should be rotating at a constant rate, not pausing for a moment every 360 degrees. Is there some way to remove the pause between loops for the animations?