How can I use the mouse to rotate an object only on x and z axis ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotate : MonoBehaviour
{
    public float speed = 1.0f;

    private void Update()
    {
        transform.Rotate(-Input.GetAxis("Mouse Y") * speed * Time.deltaTime, -Input.GetAxis("Mouse X") * speed * Time.deltaTime, 0);
    }
}

I’m setting the z axis to 0 but still when moving the mouse z is changing too. And I want that it will change only x and y.

You’re using euler angles, either switch to quaternions or use 2 objects(one a child of the other), each for one axis.

1 Like

This is working just like I wanted :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseOrbit : MonoBehaviour
{
   public float speedH = 2.0f;
   public float speedV = 2.0f;

   private float yaw = 180.0f;
   private float pitch = 0.0f;

   private void Start()
   {
       Cursor.lockState = CursorLockMode.Locked;
   }

   void Update()
   {
       yaw -= speedH * Input.GetAxis("Mouse X");
       pitch -= speedV * Input.GetAxis("Mouse Y");

       transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
   }
}