Search Unity

Question Player moving in a circle when i move my cursor horizontally.

Discussion in 'Scripting' started by MasterFailer, Jan 18, 2021.

  1. MasterFailer

    MasterFailer

    Joined:
    Jan 18, 2021
    Posts:
    4
    The error video:


    Here is the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MouseLook : MonoBehaviour
    6. {
    7.  
    8.     public float mouseSensitivity = 100f;
    9.  
    10.     public Transform playerBody;
    11.  
    12.     float xRotation = 0f;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         Cursor.lockState = CursorLockMode.Locked;
    18.     }
    19.  
    20.     // Update is called once per frame
    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, -90f, 90f);
    28.  
    29.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    30.         playerBody.Rotate(Vector3.up * mouseX);
    31.     }
    32. }

    There are no errors on console.
    There is also an attachement in this thread that shows the camera settings on inspector.
     

    Attached Files:

  2. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    Looks like your cylinder is a child of the main object (the one that's rotating), and its position is not (0,0,0) (which can be seen in the screenshot). So your object is just rotating, but the center of rotation is way off center.
     
  3. MasterFailer

    MasterFailer

    Joined:
    Jan 18, 2021
    Posts:
    4
    I really appreciate your help, it works now. I tought it was the skript. I guess im just too tired.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    This is an important point about Unity: often times code is only a tiny part of the problem, and the rest of it is how things are set up, as you see above.

    Or put another way, the most perfect set of scripts in the entire world won't work if one thing is incorrectly set up or missing in the scene / prefabs.

    Glad you're operational again!