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 looking script for FPS game tilts the whole player instead of just looking up and down

Discussion in 'Scripting' started by squishsquashh, Aug 2, 2020.

  1. squishsquashh

    squishsquashh

    Joined:
    Aug 2, 2020
    Posts:
    1
    So i have tried to make a mouse looking script for my fps game but form one day to the other my script started not to work correctly anymore. Instead of looking up and down, it was rather tilting the whole player and it was always in the same direction. My script comes from a youtuber called Brackeys and mine was exactly the same as his but it doesnt work anymore? i was also the same version as him.

    here is the script:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class MouseLook : MonoBehaviour
    7. {
    8.  
    9.     public float mouseSensitivity = 100f;
    10.  
    11.     public Transform playerBody;
    12.  
    13.     float xRotation = 0f;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         Cursor.lockState = CursorLockMode.Locked;
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    25.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    26.  
    27.         xRotation -= mouseY;
    28.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    29.  
    30.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    31.         playerBody.Rotate(Vector3.up * mouseX);
    32.     }
    33. }
    34.  
     
  2. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    I recognize the script you probably got that from Brackeys right? ;) Anyway do have the script attached to your playerBody or to your camera? Because it has to get onto the camera else you would, as with in your problem, rotate not only the camera but also the player
     
    Lethn likes this.