This code only with Right Mouse Drag?

This code rotates my model along its Y axis when I click and drag. I like how this code works, but it affect the whole scene window. So when I click a button on my interface, the model moves a little since it is set to rotate the model on mouse down. It looks like a glitch to my clients. I’m not sure what the best way to correct it would be except maybe to have this code function with a right mouse button drag? Is that possible? Does anyone have a suggestion for a different way to handle this maybe? Is there a different script that might do the same thing, but is contained to when you hover over the gameObject? I don’t know. Any help would be appreciated!

public class CamControl : MonoBehaviour 
	public int speed;
	public int friction;
	public int lerpSpeed;
	private float xDeg;
	private float yDeg;
	private Quaternion fromRotation;
	private Quaternion toRotation;

	// Update is called once per frame
	void Update () 
	        xDeg -= Input.GetAxis("Mouse X") * speed * friction;
	        yDeg += Input.GetAxis("Mouse Y") * speed * friction;
	        yDeg = Mathf.Clamp (0,0,0); //lock out to enable full spin control
	        fromRotation = transform.rotation;
	        toRotation = Quaternion.Euler(yDeg,xDeg,0);
	        transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime  * lerpSpeed);

In line 14, use Input.GetMouseDown(1). That checks for the right mouse button.


If you want to check for hovering, use Raycast with Camera.ScreenPointTo Ray and Input.mousePosition. Tag your rotating model and run these checks instead.

if(Physics.Raycast(transform.position, Camera.ScreenPointToRay(Input.mousePosition)), hit)
if(hit.collider.tag == "model")