x


FPS camera mouse script - look around only when Mouse button is pressed down?

I am using the basic FPS Camera Mouse script, that came with Unity. Is there something I can add to this script that I can make the view look around only when the Mouse Button is held down and the mouse is moved. Right now the camera moves around all the time when the mouse is moves. Thanks.

[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 ()
{
    if (axes == RotationAxes.MouseXAndY)
    {
        // Read the mouse input axis
        rotationX += Input.GetAxis("Mouse X") * sensitivityX;
        rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

        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);
}

}

more ▼

asked Sep 02 '10 at 04:20 PM

Dan Jimenez gravatar image

Dan Jimenez
20 13 13 18

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

http://unity3d.com/support/documentation/ScriptReference/Input.GetMouseButton.html

just don't execute anything like calculating the rotation or the actual rotation if it returns true

so just put everything in the Update function above into a big if statement

if (Input.GetMouseButton(0)) {
    // everything in update from the above
}
more ▼

answered Sep 02 '10 at 04:38 PM

matyicsapo gravatar image

matyicsapo
659 12 15 27

Can You show me where in the code to put

/button/ values are 0 for left button, 1 for right button, 2 for the middle button.

I am still new to scripting thanks

Sep 02 '10 at 04:42 PM Dan Jimenez

It's set up by default - 0 is Left Mouse Button, 1 is Right, 3 is Middle (I believe). So you can just put Input.GetMouseButton(0) and it means left click.

Sep 02 '10 at 07:38 PM Jason_DB

thanks Matyi, your solution worked for me.

Oct 20 '10 at 02:16 PM nom-t
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1002
x820
x50

asked: Sep 02 '10 at 04:20 PM

Seen: 6336 times

Last Updated: Sep 02 '10 at 04:20 PM