Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by ADZ_ · May 21, 2020 at 09:17 AM · cursormouselookfpscontrollermouselock

Mouse Look Cursor Issue

Hi, I am having an issue with mouse input and cursor locking. I have made a custom first person controller with mouse look all included within a single class. The mouse look works fine – except for the very first frame when the CursorLockState is first set to Locked. As far as I can tell, the cursor seems to jump from wherever it is originally positioned to the centre of the screen, which is 'technically' what is supposed to happen, but as a result the camera suddenly rotates to the new location of the mouse, which can be mildly disorientating. After the first frame, mouse input works fine.


Here is a section of code for mouse look:

 [SerializeField] private float _mouseSensitivity = 6f;
 private Vector2 _look;
 
 private void GetInput()
 {
     _look += (new Vector2(Input.GetAxisRaw("Mouse Y"), Input.GetAxisRaw("Mouse X")) * (_mouseSensitivity * 50)) * Time.fixedDeltaTime;
     _look.x = Mathf.Clamp(_look.x, -90f, 90f); // Clamps the vertical look angle so that player cannot look underneath themself
 }

 private void Look()
 {
     // Locking & hiding cursor
 
     Cursor.lockState = CursorLockMode.Locked;
     Cursor.visible = false;
 
     // Rotates self & head based on look vector
 
     transform.rotation = Quaternion.AngleAxis(_look.y, Vector3.up);
     _head.localRotation = Quaternion.AngleAxis(_look.x, Vector3.left);
 }

Both of these methods are called on Update(). The '_head' field refers to the Camera's Transform component


It appears that the further the mouse is from the centre of the screen, the further the camera uncontrollably rotates. I have tried using a few different methods to fix this, such as:

  • using LateUpdate instead of Update

  • using the Awake method

  • using CursorLockState.Confined, which causes more problems

  • using CursorLockState.None, which fixes the issue but is not what I want

  • using an IEnumerator to wait a few frames before locking the cursor

None of these approaches have worked for me.


I have looked at dozens of pages on Unity Answers and StackOverflow and have not found anything that helps me, so if anyone has any solutions please can you let me know. Thank you.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ADZ_ · May 26, 2020 at 09:16 AM

I know I shouldn't reply to my own thread, but I think I have found a solution. By adding a boolean value which is set to true after the mouse is first moved, the camera's orientation can be reset on the first frame that mouse input is received, to avoid the camera "jumping" to the mouse position.


In the unlikely event that anyone has the same problem as me, try something like this:

 bool _hasCamMoved = false;
 
 private void GetInput()
 {
     if(_hasCamMoved)
     {
         _look += (new Vector2(Input.GetAxisRaw("Mouse Y"), Input.GetAxisRaw("Mouse X")) * (_mouseSensitivity * 50)) * Time.fixedDeltaTime;
         _look.x = Mathf.Clamp(_look.x, -90f, 90f); // Clamps the vertical look angle so that player cannot look underneath themself
     }
     else
     {
         if(Input.GetAxisRaw("Mouse X") != 0 || Input.GetAxisRaw("Mouse Y") != 0)
         {
             _hasCamMoved = true;
             ResetCameraRotation(); // this stops the camera from "jumping"
         }
     }
 }
 
 private void ResetCameraRotation(bool setToCurrentOrientation = false)
 {
     if(setToCurrentOrientation) // this could be used on Awake(), although I haven't tested this yet
     {
         _look.y = transform.rotation.y;
         _look.x = _head.localRotation.x;
     }
 
     transform.rotation = Quaternion.AngleAxis(_look.y, Vector3.up);
     _head.localRotation = Quaternion.AngleAxis(_look.x, Vector3.left);
 }
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

128 People are following this question.

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

Related Questions

How to lock cursor in WebGL 1 Answer

camera locks vertical occationally 1 Answer

Disable mouselook when entering a trigger 2 Answers

Look at mouse problem 1 Answer

Mouse won't unlock in WebGL build but does in the editor 1 Answer


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