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

First person camera rotation when animating

Discussion in 'Scripting' started by Deleted User, Feb 19, 2021.

  1. Deleted User

    Deleted User

    Guest

    I'm having some problems with a player character in a first person game with a camera attached to it. It is placed in the head and the script turns the player body on.

    This is the camera script:

    Code (CSharp):
    1. float mouseSensitivity = 100f;
    2. public Transform playerBody;
    3. float xRotation = 0f;
    4.  
    5. void Update() {
    6. float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    7. float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    8.  
    9. xRotation -= mouseY;
    10. xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    11.  
    12. transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    13.  
    14. playerBody.Rotate(Vector3.up * mouseX);
    15. }
    16.  
    17.  
    All this works fine till I start with animations and the bone the camera is attached to starts moving and the camera is tilted.

    My question: how do I keep the camera steady in place? I want it to move with the head but not become tilted. Any help is greatly appreciated.
     
    Last edited by a moderator: Mar 15, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    If something is an FPS, you generally don't animate or use the full body, but a partial rig that shows what you think you want to see, like the gun, arm, hands, legs maybe, etc.

    Then you can just make a single bone that doesn't animate except to go up and down when you crouch or lean, for example.
     
  3. Deleted User

    Deleted User

    Guest

    Thank you for your reply, that all makes sense. I should have mentioned that I'm making a multiplayer game and players will be able to see eachother.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Traditionally you show the other players a full body while you see something less.
     
  5. Deleted User

    Deleted User

    Guest

    I solved it by creating an offset between the head bone and the camera and updating the camera's position based on the head bone position while not changing it rotation.
     
    Kurt-Dekker likes this.