How to rotate an object's X, Y, Z based on mouse movement

Hi there. I'm having a hard time coming up with a script that will allow me to rotate an object by all three axis based on the direction I move my mouse. To clarify a little more for example: no matter what position on it's X, Y, and Z it is in, I'd like the object in to rotate downward and toward me upon moving the mouse downward, rotate leftward no matter it's X, Y, Z position if i move my mouse leftward, ETC.

Edit: I worked around this problem by using physics to my advantage: Two positions to apply force makes the object spin as if my mouse is controlling it. Here's the script. If there are no better answers in the next few days I'll close this as solved.

function Update () { rigidbody.AddForceAtPosition(Vector3(transform.position.x+(10*Input.GetAxis("Mouse X")),transform.position.y,transform.position.z+(10*Input.GetAxis("Mouse Y"))),Vector3(transform.position.x,transform.position.y+4,transform.position.z)); print(transform.rotation); }

Why not just use transform.Rotate()?

 var speed : float = 1.0; //how fast the object should rotate

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

Best wishes,
-Lincoln Green

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public float horizontalSpeed = 2.0F;
    public float verticalSpeed = 2.0F;
    void Update() {
        float h = horizontalSpeed * Input.GetAxis("Mouse X");
        float v = verticalSpeed * Input.GetAxis("Mouse Y");
        transform.Rotate(v, h, 0);
    }
}

float yawMouse = Input.GetAxis(“Mouse X”);
float pitchMouse = Input.GetAxis(“Mouse Y”);
Vector3 targetFlyRotation = Vector3.zero;

    Screen.lockCursor = true;

    if (Mathf.Abs(yawMouse) > 0.1f || Mathf.Abs(pitchMouse) > 0.1f)
    {
        targetFlyRotation = yawMouse * transform.right + pitchMouse * transform.up;
        targetFlyRotation.Normalize();
        targetFlyRotation *= Time.deltaTime * 3.0f;

        //limit x rotation if looking too much up or down
        //Log out the limitX value for this to make sense
        float limitX = Quaternion.LookRotation(moveDirection + targetFlyRotation).eulerAngles.x;

            //70 sets the rotation limit in the down direction
            //290 sets limit for up direction
        if (limitX < 90 && limitX > 70 || limitX > 270 && limitX < 290)
        {
            Debug.Log("restrict motion");
        }
        else
        {
            moveDirection += targetFlyRotation;
           //does the actual rotation on the object if no limits are breached
            transform.rotation = Quaternion.LookRotation(moveDirection);
        }

    }

Hi and thx Tim,

may i ask u something to the suggested solution? i tried but didnt get it :frowning:

heres my code so fare:
public GameObject Target;
public float horizontalSpeed = 2.0F;
public float verticalSpeed = 2.0F;
public float angle = 45.0f;

   void Update()
        {
     
        
     
        if (Input.GetMouseButton(0))

        {
            MouseSpin();
        }

        if (Input.GetMouseButtonDown(1))

        {
           Target.transform.rotation = Quaternion.identity;
        }
     
    }

    void MouseSpin()
    {
        
        float h = horizontalSpeed * Input.GetAxis("Mouse X");
        float v = verticalSpeed * Input.GetAxis("Mouse Y");
        v = Mathf.Clamp(v, 1, -1);
        Target.transform.Rotate(v, h, 0);
        
        
    }


}

the clamp doesnt work ^^ maybe im just to dumb, what did i wrong?

greets

If anyone stumbles on this, could be fun to check this out!