- Home /
restrict mouselook X movement script does not work
Why is it that I can restrict the mouseY movement (min and max) in the mouselook script that comes with Unity in the standard assets, but the min and max of the mouseX does not work? Any help would be great, thanks!
Answer by alexmon27 · Feb 13, 2011 at 03:11 AM
AH! Just figured it out. Anyone who wants a quick fix, feel free to copy this code... Open up the mouselook script and change this...
float rotationY = 0F;
void Update () { if (axes == RotationAxes.MouseXAndY) { float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
To this...
float rotationX = 0F; float rotationY = 0F;
void Update () { if (axes == RotationAxes.MouseXAndY) { rotationX += Input.GetAxis("Mouse X") * sensitivityX; rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
I think it's a little ridiculous how they scripted this script by using variables that are never put into use. Hopefully one of the developers will get a glance of this and fix it in the next release :)
This is not a real fix as it's not possible to make a full rotation (like in classic FPS games) anymore. The camera will be locked to -360 +360 degrees.
No prob, Potato :) And the old script that I edited allowed me to make full rotations if I reset the rotation at max. The script was only for a menu.
Answer by lvictorino · Jun 01, 2012 at 03:52 PM
It seems important to allow the camera to do full rotations on X axis if needed. Something like that will allow it ( if minimumX
equals maximumX
):
if (axes == RotationAxes.MouseXAndY) {
if (minimumX == maximumX) {
rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
}
else {
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationX = Mathf.Clamp(rotationX, minimumX, maximumX);
}
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
Hi all, I$$anonymous$$HO it makes more sense to use this condition
if ($$anonymous$$athf.Abs($$anonymous$$imumX) == 360F && $$anonymous$$athf.Abs(maximumX) ==360F)
for the full rotation.
Answer by Nebuch · Jun 25, 2015 at 12:58 PM
I have debug the script :)
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Look (Debugged Edition)")]
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;
public bool smoothLookX;
private float sensivityXmemory;
private float maximumSmoothX;
private float minimumSmoothX;
float rotationY = 0F;
float rotationX = 0F;
void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
//float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
//NEBUCH
if (smoothLookX) {
if (rotationX > maximumSmoothX) {
sensitivityX = 1f;
}
if (rotationX < minimumSmoothX){
sensitivityX = 1f;
}
if (rotationX > minimumSmoothX && rotationX < maximumSmoothX){
sensitivityX = sensivityXmemory;
}
}
}
void Start ()
{
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>())
GetComponent<Rigidbody>().freezeRotation = true;
//NEBUCH
sensivityXmemory = sensitivityX;
maximumSmoothX = maximumX - maximumX / 5;
minimumSmoothX = minimumX + minimumX / (-5);
}
}
I added an option for inverting Y-axis mouse look.
public bool invertX;
public bool invertY;
void Update () {
if (axes == RotationAxes.$$anonymous$$ouseXAndY) {
//float rotationX = transform.localEulerAngles.y + Input.GetAxis("$$anonymous$$ouse X") * sensitivityX;
rotationX += Input.GetAxis ("$$anonymous$$ouse X") * sensitivityX;
rotationY += Input.GetAxis ("$$anonymous$$ouse Y") * sensitivityY;
rotationY = $$anonymous$$athf.Clamp (rotationY, $$anonymous$$imumY, maximumY);
rotationX = $$anonymous$$athf.Clamp (rotationX, $$anonymous$$imumX, maximumX);
if (invertX && invertY) {
transform.localEulerAngles = new Vector3 (rotationY, -rotationX, 0);
} else if (invertX) {
transform.localEulerAngles = new Vector3 (-rotationY, -rotationX, 0);
} else if (invertY) {
transform.localEulerAngles = new Vector3 (rotationY, rotationX, 0);
} else {
transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
}
} else if (axes == RotationAxes.$$anonymous$$ouseX) {
if(invertX) {
transform.Rotate (0, Input.GetAxis ("$$anonymous$$ouse X") * -sensitivityX, 0);
} else {
transform.Rotate (0, Input.GetAxis ("$$anonymous$$ouse X") * sensitivityX, 0);
}
} else {
rotationY += Input.GetAxis ("$$anonymous$$ouse Y") * sensitivityY;
rotationY = $$anonymous$$athf.Clamp (rotationY, $$anonymous$$imumY, maximumY);
if (invertY) {
transform.localEulerAngles = new Vector3 (rotationY, transform.localEulerAngles.y, 0);
} else {
transform.localEulerAngles = new Vector3 (-rotationY, transform.localEulerAngles.y, 0);
}
}
}