Mouse look at (holding R button down)

Hey - I like using the mouse look-at script.

It moves the direction you’re looking at by just moving the mouse.

But right now I would like the mouse look-at to work only when you are holding down the right mouse button AND moving the mouse.

Does anyone have any idea how to set this up?

Thanks

add

if(Input.GetMouseButton(1))

to the script

I did something similar to this a couple weeks ago. What I did was use states. I made one state for looking around and another for walking or whatnot. In your case it would end up being

if (Input.GetButton(1)) {
     State = Looking // I'm not using perfect syntax because you could look that up on your own.
}

then in your player script

if (State == Looking) {
 Mouse Around Stuff...
}

Wow! you photo shocked me!!! I did not they wrote a lot of code in Kazakhstan :open_mouth:

Here is the mouse look at script. I inserted the line of code here… is that where it goes?

using UnityEngine;
using System.Collections;

[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {

	public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
	public RotationAxes axes = RotationAxes.MouseXAndY;
	public float sensitivityX = 15F;
	public float sensitivityY = 15F;

	public float minimumX = -360F;
	public float maximumX = 360F;

	public float minimumY = -60F;
	public float maximumY = 60F;

	float rotationX = 0F;
	float rotationY = 0F;
	
	Quaternion originalRotation;

	void Update ()
	{
	[b]if(Input.GetMouseButton(1))[/b] //// here is where I put your line of code Borat 		if (axes == RotationAxes.MouseXAndY)
		{
			// Read the mouse input axis
			rotationX += Input.GetAxis("Mouse X") * sensitivityX;
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
			
			// if(Input.GetAxis("Horizontal")  !Input.GetAxis("Vertical"))

			rotationX = ClampAngle (rotationX, minimumX, maximumX);
			rotationY = ClampAngle (rotationY, minimumY, maximumY);
			
			Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
			Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
			
			transform.localRotation = originalRotation * xQuaternion * yQuaternion;
		}
		else if (axes == RotationAxes.MouseX)
		{
			rotationX += Input.GetAxis("Mouse X") * sensitivityX;
			rotationX = ClampAngle (rotationX, minimumX, maximumX);

			Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
			transform.localRotation = originalRotation * xQuaternion;
		}
		else
		{
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
			rotationY = ClampAngle (rotationY, minimumY, maximumY);

			Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
			transform.localRotation = originalRotation * yQuaternion;
		}
	}
	
	void Start ()
	{
		// Make the rigid body not change rotation
		if (rigidbody)
			rigidbody.freezeRotation = true;
		originalRotation = transform.localRotation;
	}
	
	public static float ClampAngle (float angle, float min, float max)
	{
		if (angle < -360F)
			angle += 360F;
		if (angle > 360F)
			angle -= 360F;
		return Mathf.Clamp (angle, min, max);
	}
}

Very nice!

now you only need to add curly brackets

void Update () 
   { 
   [b]if(Input.GetMouseButton(1)) {//added curly bracket here

[/b] //// here is where I put your line of code Borat       if (axes == RotationAxes.MouseXAndY) 
      { 
         // Read the mouse input axis 
         rotationX += Input.GetAxis("Mouse X") * sensitivityX; 
         rotationY += Input.GetAxis("Mouse Y") * sensitivityY; 
          
         // if(Input.GetAxis("Horizontal")  !Input.GetAxis("Vertical")) 

         rotationX = ClampAngle (rotationX, minimumX, maximumX); 
         rotationY = ClampAngle (rotationY, minimumY, maximumY); 
          
         Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up); 
         Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left); 
          
         transform.localRotation = originalRotation * xQuaternion * yQuaternion; 
      } 
      else if (axes == RotationAxes.MouseX) 
      { 
         rotationX += Input.GetAxis("Mouse X") * sensitivityX; 
         rotationX = ClampAngle (rotationX, minimumX, maximumX); 

         Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up); 
         transform.localRotation = originalRotation * xQuaternion; 
      } 
      else 
      { 
         rotationY += Input.GetAxis("Mouse Y") * sensitivityY; 
         rotationY = ClampAngle (rotationY, minimumY, maximumY); 

         Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left); 
         transform.localRotation = originalRotation * yQuaternion; 
      } 
   } 
}//added curly bracket here

now that I think about it, this might not work actually. You have to try it out. If it doesn’t work we need to change it so that it uses the change in mouse position.

Illya like curly brackets…

However - like woman - brain size of squirrel…

My character only turns in one direction - if I set the input ‘mouse 1’ positive she turns right - negative turns left … if I set both she won’t turn at all

works for me. Be careful not to comment out something. Also are you setting up your character like it is explained in the script? with a first person controller that has Axes set to MouseX and then a camera attached to the first person controller that has the Axes set to MouseY in the MouseLook script.
I am attaching the script for your convenience

391790–13454–$mouselook_211.cs (2.34 KB)

I got it!!!

Illya so happy feels like Pamala Anderson!!!

Thanks :slight_smile: