Click and drag to rotate an object without changing its position.

I’m trying to rotate an object ( a cube for now ) like a moving aeroplane on its z axis and y axis by using a mouse click and mouse drag.

i want the rotations to be like the code below but only on mouse drag.
I’m not sure how to use the mouse drag event a simple pseduocode or directions to do so would be really helpful.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {

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

}

i made a few changes and tried the following

if(Input.GetMouseButton(0)){
    	 h = horizontalSpeed * Input.GetAxis("Mouse X");
    	 print (h);
    	//transform.Rotate(0, h, 0);
		}
	else if(Input.GetMouseButton(1)){
		v = verticalSpeed * Input.GetAxis("Mouse Y");
		print(v);
    	//transform.Rotate(0, 0, v);
	}
		
	transform.Translate(0,0,speed/20*Time.deltaTime);
}

}

now the cube moves forward and rotates only when i drag the mouse on the right side and vertically upwards rotation but does not respond when i try vertically downwards or on its left…also when i print the val i see -ve values but doesn’t rotate…

~thank you.

a OnMouseDrag function exists already in unity, maybe you can use that?
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseDrag.html

Lol, i got it working the mistake in the previous code was i’m rotating on z axis instead of the x axis.
this will work for flying a plane object with the mouse to some extent.

using UnityEngine;

using System.Collections;

public class PlaneMovement : MonoBehaviour {
public float horizontalSpeed = 2.0F;
public float verticalSpeed = 2.0F;
float h;
float v;
float speed = 500f;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
	
	if(Input.GetMouseButton(0)){
    	 h = horizontalSpeed * Input.GetAxis("Mouse X");
    	 print (h);
    	transform.Rotate(0, h, 0);
		}
	
	else if(Input.GetMouseButton(1)){
		v = verticalSpeed * Input.GetAxis("Mouse Y");
		print(v);
    	transform.Rotate(v, 0, 0);
	}
		
	transform.Translate(0,0,speed/20*Time.deltaTime);
}

}