#Camera rotating when placed as a child of the characters head.

1 messages · Page 1 of 1 (latest)

wind moss
#

So I got a character but when I try to place the camera as a child for it's head and then start game, the camera rotates for some reason.

#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerLook : MonoBehaviour
{
    [SerializeField] private float sensX;
    [SerializeField] private float sensY;

    Camera cam;

    float mouseX;
    float mouseY;

    float multiplier = 0.01f;

    float xRotation;
    float yRotation;

    private void Start()
    {
        cam = GetComponentInChildren<Camera>();

        sensX = 400f;
        sensY = 400f;

        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;

    }

    private void Update()
    {
        MyInput();



        cam.transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
        transform.rotation = Quaternion.Euler(0, yRotation, 0);

        
            
    }

    void MyInput()
    {
        mouseX = Input.GetAxisRaw("Mouse X");
        mouseY = Input.GetAxisRaw("Mouse Y");

        yRotation += mouseX * sensX * multiplier;
        xRotation -= mouseY * sensY * multiplier;

        xRotation = Mathf.Clamp(xRotation, -120f, 80f);
    }
}``` The code of the camera
#

^ Rotation before starting the game

wheat whale
#

if any of those objects in that chain of parent/child relationships rotates that will make the camera rotate as well. For example, if you have some sort of idle animation that slightly rotates the head, then your camera will rotate along with it because its rotation will be dependent on the parent's rotation

wind moss
#

After starting the game

wind moss
#

Here is the rotation of the head before the game

wheat whale
#

either way, if unparenting it makes it not rotate like that, that would be the best option

wind moss
wind moss
#

or when the face moves left and right I want the camera to move aswell

wheat whale
#

well for one, your camera's rotation is based on the parent's rotation. The head is clearly rotated incorrectly by default because its default rotation is apparently sideways. You are setting the camera's rotation to 0 0 0 on the first Update because this line does: cam.transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
it's still best not to make the camera a child of the player because you run into issues where the camera will move in ways you do not want. But if you want to take control of the camera away from the player by making animations drive the rotation of the camera rather than mouse movement, then be my guest. But just keep in mind your camera shouldn't be a child of the player

wind moss
#

I am not good at english can you explain what you mean by "But if you want to take control of the camera away from the player by making animations drive the rotation of the camera rather than mouse movement, then be my guest. But just keep in mind your camera shouldn't be a child of the player"

#

Sorry btw