Locking quaternion on one axis?

Hello! :slight_smile: i have this code: which rotates the rigidbody towards a gameobjects position. Bare in mind that i have to rotate the rigidbody on the Y-axis only and the camera on the X axis later…

var qTo = Quaternion.LookRotation(test.transform.position - transform.position);
            qTo = Quaternion.Slerp(transform.rotation, qTo, 10f * Time.deltaTime);
           
            _rigidbody.MoveRotation(qTo);

But this does rotate the rigidbody on all axis (which is obvious)
Now to the point.

I have tried subtracting from the Quaternion or making a new quaternion like:

new Quaternion(0, qTo.y, 0, qTo.w);

But this gives weird results, as Quaternions are really weird to understand heheh :roll_eyes:

So how can i make it rotate on the Y-axis only?

I do have rigidbody rotation constraints set on X and Z but the code seems to override this.

1 Like

Usually best to do two transforms parented to each other. Here’s an example of a battleship turret.

Turret aiming/rotating:

https://discussions.unity.com/t/783469/2

Hmmm :face_with_spiral_eyes: i don’t understand :C

Let me explain a little better as well:

  • My game is first-person / FPS
  • Rigidbody rotates on the Y axis only, to allow me to look left/right with the mouse
  • Camera which is a child of the rigidbody rotates on the X-axis up and down with the mouse
  • …
  • Now i need to snap to a specific point with my crosshair/aim in the world.
  • I disable the mouse input so i cannot rotate with the mouse.
  • Using Quaternion.Slerp gives me good results, BUT! when walking too close to the gameobject i snap my look to, i also rotate the whole rigidbody on all axis so it “lays down” on the ground as well, which i don’t want :eyes:
1 Like

Does this help? how do i use Quaternion lerp on only the Y and Z axis
I couldn’t get it to work for me, and ended up doing this, which worked alright, but not perfect when up close. It’s at least only spinning on y-axis:

if (!EnableSpiningVertical){
   rot = new Quaternion(0,rot.y,0,rot.w);
}

I think this could also be useful: Set rotation on single axis with provided angle using Quaternion - Game Development Stack Exchange