- Home /
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); }
}
Answer by matyicsapo · Sep 02, 2010 at 04:38 PM
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
}
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
It's set up by default - 0 is Left $$anonymous$$ouse Button, 1 is Right, 3 is $$anonymous$$iddle (I believe). So you can just put Input.Get$$anonymous$$ouseButton(0) and it means left click.
This would be your new Update function Everything inside Update's two curly braces is pasted the te commented-out line.
void Update ()
{
if (Input.Get$$anonymous$$ouseButton(0)) {
// everything in update from the above
}
}
Please mark the answer by matyscsapo as accepted if it works for you (which it should).
Your answer
Follow this Question
Related Questions
GUI Button OnMouseUp? 2 Answers
OnMouseDown() Doesn't work 17 Answers
MouseDown on GUI.button 4 Answers
Detect Mouse Click OnGUI 1 Answer
Help with resize script 1 Answer