Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by alexmon27 · Feb 13, 2011 at 03:09 AM · rotationmousemouselooklookrestrict

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PotatoOrgyLt · Mar 16, 2012 at 07:53 PM 0
Share

Thank you for fixing the script it really helped!

avatar image lvictorino · Jun 01, 2012 at 03:44 PM 0
Share

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.

avatar image alexmon27 · Jun 24, 2012 at 04:09 AM 0
Share

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.

avatar image gooncorp · Aug 06, 2012 at 11:09 PM 0
Share

thanks you just saved me 78.4 hours on my project.

avatar image Slulego · Jan 06, 2013 at 02:08 AM 0
Share

THAN$$anonymous$$ YOU!!!

Show more comments
avatar image
1

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);
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jeango · Dec 20, 2012 at 08:15 AM 0
Share

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.

avatar image
1

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ownerfate · Jul 25, 2015 at 03:10 PM 0
Share

Thanks Nebuch

avatar image wallacewakko · Dec 05, 2015 at 06:07 PM 0
Share

Woow really good! Thank you! :D

avatar image JBehreandt · Apr 16, 2016 at 11:59 AM 0
Share

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

}

avatar image
0

Answer by paddyb180285 · Mar 26, 2013 at 03:21 PM

That worked a treat "alexmon27", thanks.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Plane.Raycast for mouselook won't work in positive y-axis 1 Answer

Geometry Wars game experience in Unity 2 Answers

Player Torso Rotation to mouselook. (Diagram included) 0 Answers

Using quaternion for mouse movement? 3 Answers

draw line between mouse and gameobject on flat plane 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges