P
P
password222020-12-24 23:46:29
Unity
password22, 2020-12-24 23:46:29

How to make the player's head follow the camera rotation?

Good afternoon! I just can't figure out how to make it so that when the camera turns left and right, the player turns with it (as it is now), but how can I make only the player's head turn up and down? With such a script, the entire body of the player rotates, as if being in front of the camera.

I will be glad for any help. Thanks in advance

public FixedJoystick _joystickMove;
    public DynamicJoystick _joystickDirection;

    CharacterController _character;
    Vector3 move = Vector3.zero;
    float gravity = -9.81f;

    public float minX = -360.0f;
    public float maxX = 360.0f;

    public float minY = -45.0f;
    public float maxY = 45.0f;

    public float sensX = 120.0f;
    public float sensY = 120.0f;

    float rotationY = 0.0f;
    float rotationX = 0.0f;

    private void Start()
    {
        _character = GetComponent<CharacterController>();
    }

    private void Update()
    {
        PlayerMovement();
    }

    private void PlayerMovement()
    {
        // этот джойстик за движение персонажа
        float hMove = _joystickMove.Horizontal;
        float vMove = _joystickMove.Vertical;

        // этот джойстик отвечает за повороты камеры
        float hDirection = _joystickDirection.Horizontal;
        float vDirection = _joystickDirection.Vertical;

        move = new Vector3(hMove, 0, vMove);
        move = Camera.main.transform.TransformDirection(move);
        move.y = gravity * Time.deltaTime;
        _character.Move(move * 0.5f);

        //поворот камеры вместе с игроком
        rotationX += hDirection * sensX * Time.deltaTime;
        rotationY += vDirection * sensY * Time.deltaTime;
        rotationY = Mathf.Clamp(rotationY, minY, maxY);
        
        // нужно чтобы вверх и вниз крутилась только голова, не сам персонаж

        transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question