Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Why does my script not work???

Discussion in 'Scripting' started by kaysteinhoff2003, May 30, 2020.

  1. kaysteinhoff2003

    kaysteinhoff2003

    Joined:
    Mar 22, 2020
    Posts:
    19
    So... I am working on an flight simulator and as I tried to implemend the first-person-view I wrote this scipt. I wanted that you can see 90 deg to your left and right and up and down. to me it looks like I've done the exact same thing for the y axis as for the x but as it seem I haven't.

    Here's the code I wrote

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class MouseLook : MonoBehaviour
    5. {
    6.    
    7.     public float mouseSensitivity = 100f;
    8.     float xRotation = 0f;
    9.     float yRotation = 0f;
    10.    
    11.     public Transform playerBody;
    12.    
    13.     void Start()
    14.     {
    15.         Cursor.lockState = CursorLockMode.Locked;
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    21.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    22.        
    23.         xRotation -= mouseY;
    24.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    25.         yRotation -= mouseX;
    26.         yRotation = Mathf.Clamp(yRotation, -90f, 90f);
    27.        
    28.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    29.         transform.localRotation = Quaternion.Euler(0f, yRotation, 0f);
    30.     }
    31. }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Hey and welcome.
    Generally speaking it's a good idea to mention what is not working. Are you getting errors? What is the expected behavior? What is the desired behavior? More information is always good.

    One thing i see at first glance is that you set localRotation to something, then set it to something else. The first assignment (line 28) is pointless, since the other assignment (line 29) simply overwrites it. So you'd most likely want to fix that by putting both xRotation and yRotation in the same Quaternion.Euler(..).
     
  3. kaysteinhoff2003

    kaysteinhoff2003

    Joined:
    Mar 22, 2020
    Posts:
    19
    I'm so glad that you made me aware of this error! Ihad no errors in the consol what made me didn't think about that. Thanks for the answer otherwise I probably would have sit here for hours :)

    I thought maybe someone could need the script so here is the optimized version:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MouseLook : MonoBehaviour
    4. {
    5.    
    6.     public float mouseSensitivity = 100f;
    7.     public float xMinAngle = -90f;
    8.     public float xMaxAngle = 90f;
    9.     public float yMinAngle = -90f;
    10.     public float yMaxAngle = 90f;
    11.     float xRotation = 0f;
    12.     float yRotation = 0f;
    13.    
    14.     public Transform playerBody;
    15.    
    16.     void Start()
    17.     {
    18.         Cursor.lockState = CursorLockMode.Locked;
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    24.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    25.      
    26.         xRotation -= mouseY;
    27.         xRotation = Mathf.Clamp(xRotation, xMinAngle, xMaxAngle);
    28.         yRotation += mouseX;
    29.         yRotation = Mathf.Clamp(yRotation, yMinAngle, yMaxAngle);
    30.      
    31.         transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);
    32.     }
    33. }