0
votes

I try to make a 3rd person camera, which follows my player and the camera should rotate, but not the player, if I use the right analog Stick of my controller. I followed this tutorial

My code:

void adjustCameraToPlayer() 
{
    Quaternion rotation = Quaternion.identity;
    if (Input.GetAxis("RightStickX") != 0f)
    {
        float horizontal = Input.GetAxis("RightStickX") / 100f;
        transform.Rotate(0, horizontal, 0);
        float desiredAngle = transform.eulerAngles.y;
        rotation = Quaternion.Euler(0, desiredAngle, 0);
    }

    transform.position = player.transform.position-(rotation * offset);

    transform.LookAt(player.transform);
}

My problem is that the camera rotates way too fast, I tried to change the dividend of the horizontal value but it did not help.

1

1 Answers

2
votes

That's why you always should incorporate deltaTime into transform operations that happen every frame. That way you aren't rotating it at magnitude every single frame, but instead over time. Also you should incorporate a speed variable which can be manipulated in real time, so you can tweak it just how you want:

public float speed = 5f;

void adjustCameraToPlayer() 
{
    Quaternion rotation = Quaternion.identity;
    if (Input.GetAxis("RightStickX") != 0f)
    {
        float horizontal = Input.GetAxis("RightStickX");
        transform.Rotate(Vector3.up * horizontal * speed * Time.deltaTime);
        float desiredAngle = transform.eulerAngles.y;
        rotation = Quaternion.Euler(0, desiredAngle, 0);
    }

    transform.position = player.transform.position-(rotation * offset);

    transform.LookAt(player.transform);
}