Moving object in one direction and rotating around own axis at the same time

Hi guys,

I am doing the Space Shooter tutorial and have been tweaking some parts here and there.

For example, I have replaced the supplied laserbolt (which only moves upward in the tutorial) with a small triangle that should move upwards along the y axis as well. It should, however, also rotate around its own z axis so that it looks more like a shuriken.

For the movement upwards (along the y axis) I am using (in FixedUpdate):

rigidbody.velocity = transform.up * speed;

For the rotation I have tried several things, for example:

rigidbody.rotation = Quaternion, Euler(0, 0, 20);

The problem with this is that it also affects the x-coordinates. How can I make the triangle object rotate around its own z-axis while still having it fly upwards in parallel to the y axis?

Thanks

You could use localEulerAngles

There are 2 ways to do this:

  1. Replace rigidbody.velocity = transform.up * speed; with rigidbody.velocity = Vector3.up * speed;
    This will make it so that it always goes in the up direction regardless of how it’s rotated.

or

  1. Put the rotating bit as a child to the main object. Move the main object while rotating the child. The parent can just be an empty object.

Thanks a lot! I have changed it to rigidbody.velocity = Vector3.up * speed and now that is working.
I have created a prefab for the shot but now the rotation is not applied at all for the instantiated objects…

Got it, it is working!

1 Like