x


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!

more ▼

asked Feb 13 '11 at 03:09 AM

alexmon27 gravatar image

alexmon27
28 3 3 7

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

3 answers: sort voted first

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 :)

more ▼

answered Feb 13 '11 at 03:11 AM

alexmon27 gravatar image

alexmon27
28 3 3 7

Thank you for fixing the script it really helped!

Mar 16 '12 at 07:53 PM PotatoOrgyLt

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.

Jun 01 '12 at 03:44 PM lvictorino

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.

Jun 24 '12 at 04:09 AM alexmon27

thanks you just saved me 78.4 hours on my project.

Aug 06 '12 at 11:09 PM gooncorp

THANK YOU!!!

Jan 06 at 02:08 AM Slulego
(comments are locked)
10|3000 characters needed characters left

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

answered Jun 01 '12 at 03:52 PM

lvictorino gravatar image

lvictorino
639 7 17 19

Hi all, IMHO it makes more sense to use this condition

if (Mathf.Abs(minimumX) == 360F && Mathf.Abs(maximumX) ==360F)

for the full rotation.

Dec 20 '12 at 08:15 AM jeango
(comments are locked)
10|3000 characters needed characters left

That worked a treat "alexmon27", thanks.

more ▼

answered Mar 26 at 03:21 PM

paddyb180285 gravatar image

paddyb180285
1

(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:

x2163
x983
x162
x87
x25

asked: Feb 13 '11 at 03:09 AM

Seen: 2041 times

Last Updated: Mar 26 at 03:21 PM