0

I'm making a 3rd person controller in unity and i'm stuck: i can't figure out how to make the player follow my freelook cinemachine.

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed;
    public GameObject Hoverboard_script;
    public GameObject cinemachine_Hoverboard;
    public Transform main_cam_tf;

    void Update()
    {
        Hoverboard_script.SetActive(false);
        cinemachine_Hoverboard.SetActive(false);
        PlayerMovement_void();
        CamController_void();
    }

    void PlayerMovement_void()
    {
        float hor = Input.GetAxisRaw("Horizontal");
        float ver = Input.GetAxisRaw("Vertical");
        Vector3 playerMovement = new Vector3(hor, 0f, ver).normalized * speed * Time.deltaTime;
        transform.Translate(playerMovement, Space.Self);
    }

    void CamController_void()
    {
        float MouseY = main_cam_tf.eulerAngles.y;
        Debug.Log(MouseY);
        Quaternion rotation = Quaternion.Euler(0f, MouseY, 0f);
        transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * 4f);
    }
}

Right now if i rotate the camera, the player enters a non-stop loop where it rotate constantly. How can i solve? Thanks, cheers

fanti
  • 1
  • 2

1 Answers1

0

If you want to rotate the player as the rotation value of Y of Camera then remove all the lines of codes in which they rotate your object. Instead use one of these:

1.

transform.rotation =  Quaternion.AngleAxis(Camera.Main.transform.eulerAngles.y, Vector3.up);
transform.rotation = Quaternion.EulerAngles(transform.eulerAngles.x, Camera.Main.transform.eulerAngles.y, transform.eulerAngles.z);
Sven Viking
  • 2,660
  • 21
  • 34