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. Dismiss Notice

Rotating when moving head

Discussion in 'Scripting' started by ChipotleGod, Sep 2, 2020.

  1. ChipotleGod

    ChipotleGod

    Joined:
    Jul 20, 2020
    Posts:
    48
    When I try to just look around my player rotates around in a circle. It moves when I look around. It completely switches locations. Here is my code for looking around.
    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.  
    31.         playerBody.Rotate(Vector3.up*mouseX);
    32.  
    33.         }
    34. }
    35.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    You have a lot of double-action going on in that script above that makes no obvious sense to me.

    For one you are setting x rotation to the Y input. That's weird.

    For two you are driving the script game object's local rotation to a fixed rotation in line 29. Then you are relatively rotating ANOTHER transform around the global +Y axis by the mousex value.

    Perhaps you want to review any one of the hundreds of youtube character controller scripts out there, see how it is traditionally accomplished. The steps are basically two or three:

    - gather input
    - filter input (smooth, scale, etc.)
    - apply input to rotation, either positionally (absolute) or relatively (like a console controller)
     
  3. ChipotleGod

    ChipotleGod

    Joined:
    Jul 20, 2020
    Posts:
    48
    See, since im a noob, im not very smart with this stuff. So could you kinda dumb it down. If you dont want to I completley understand.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    No need! There are literally hundreds of youtube tutorials for character control, and 100% of them are gonna be better than anything I can type here to you. Google is your good friend learning Unity3D.
     
  5. ChipotleGod

    ChipotleGod

    Joined:
    Jul 20, 2020
    Posts:
    48
    Oh ok. What do I look up?
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @ChipotleGod

    I guess you are creating FPS capsule controller...?

    I think the code should work, maybe you have simply placed the camera incorrectly, not centered inside the capsule where head/eyes are located.
     
    PraetorBlue likes this.
  7. ChipotleGod

    ChipotleGod

    Joined:
    Jul 20, 2020
    Posts:
    48
    Yeah I was thinking about that. Hold on.
     
  8. ChipotleGod

    ChipotleGod

    Joined:
    Jul 20, 2020
    Posts:
    48
    Its not the camera. When I try to move and look around my character moves like it is attached to a circle.
     
  9. ChipotleGod

    ChipotleGod

    Joined:
    Jul 20, 2020
    Posts:
    48
    How do I change that?
     
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    It sounds like the pivot point of your character (whatever
    playerBody
    is) is not at the center of mass for that object. Are you using Unity primitive shapes, or a custom mesh, or what? What does your object hierarchy look like?
     
  11. ChipotleGod

    ChipotleGod

    Joined:
    Jul 20, 2020
    Posts:
    48
    Here ya go
     

    Attached Files:

  12. ChipotleGod

    ChipotleGod

    Joined:
    Jul 20, 2020
    Posts:
    48
    So what do I do?