Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Mouse Rotation + Screen Resolution

Discussion in 'Scripting' started by Deleted User, Jul 26, 2017.

  1. Deleted User

    Deleted User

    Guest

    Hi!

    I am facing a weird problem. I have a Mouse Look script for a First Person view. When I am working in the Editor everything is working as expected. But when I build everything to a Windows Player and change the resolution, the mouse rotation is slower and more laggy. First I thaught there is a problem with Time.deltaTime, but it seems I made another mistake.

    As I am working on a Viewer software, which will be run in Window Mode and where the User can change the resolution on runtime, this is quiet annoying. Here is the code for the Mouse Look (part from a bigger script):

    Code (CSharp):
    1.    
    2.     private float rotX = 0.0f;
    3.     private float rotY = 0.0f;
    4.     private float rotZ = 0.0f;
    5.  
    6.     void Update() {
    7.         rotationX = Input.GetAxisRaw("Mouse X") * Time.deltaTime;
    8.         rotationY = -Input.GetAxisRaw("Mouse Y") * Time.deltaTime;
    9.     }
    10.    
    11.     void FixedUpdate() {
    12.      Rotate(rotationY, rotationX, 0.0f, false);
    13.     }
    14.  
    15.     private void Rotate(float x, float y, float z, bool firstPerson)
    16.     {
    17.         rotX += x * mouseSpeed;
    18.         rotY += y * mouseSpeed;
    19.         rotZ += z * mouseSpeed;
    20.  
    21.         if (firstPerson)
    22.         {
    23.             rb.transform.Rotate(rotX, rotY, rotZ);
    24.         }
    25.         else
    26.         {
    27.             var camTransform = currentCamera.GetComponentInChildren<Camera>().transform;
    28.             currentCamera.transform.forward = new Vector3(
    29.                 camTransform.forward.x,
    30.                 0.0f,
    31.                 camTransform.forward.z
    32.             );
    33.  
    34.             camTransform.Rotate(rotX, rotY, rotZ);
    35.         }
    36.     }
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    use the screen resolution in your update.

    Code (csharp):
    1.  
    2. Input.GetAxisRaw("Mouse X") / Screen.Width * Time.deltaTime * 100;
    3.  
    This converts the axis to a percentage of the screen rather than pixels.
     
  3. Deleted User

    Deleted User

    Guest

    Thanks for this information.

    This makes the movement smoother on different resolutions (because I use a resizable player window), but I am now facing another problem. When I use the mouse, the camera begins to spin until I move the mouse back to the "zero point". Its like I am using the mouse like a stick on a controller. Once I put the stick in one direction the camera spins, until I push the stick back in the other direction.

    What I want to do is to move the mouse and when I stop it, the camera rotation should also stop.
    I guess the problem is somewhere here:

    Code (CSharp):
    1.  
    2. rotX += x * mouseSpeed;
    3. rotY += y * mouseSpeed;
    4. rotZ += z * mouseSpeed;
    5.  
    because I am adding the value to my rotation values. Any ideas?
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    so, the line after that, should be..

    Code (csharp):
    1.  
    2. mouseSpeed = 0;
    3.  
     
  5. Deleted User

    Deleted User

    Guest

    This eliminates every movement speed. The variable mouseSpeed controls the sensitivity. If I set it to zero, my character won't rotate anymore.