1
\$\begingroup\$

I have a camera follow script and I am rotating the camera with Q and E key to right and left. The Camera is a child of a camHolder object. I came up with this code, which rotates around the player to the right and left, but it's shaking as it tries to keep rotating around the player.

I didn't come up with a solution to stop the RotateAround function, instead I tried setting rotateSpeed to 0 after I let go of the key, but that didn't work either.


public Transform player;
public float smoothSpeed = 0.125f;
public float rotateSpeed;
private Vector3 velocity = Vector3.zero;

[SerializeField]
private Vector3 offset;

[Space]
public Transform camHolder;

void LateUpdate () 
{
    CameraFollowPlayer();
}

void Update()
{
    RotateCamera();
}

void CameraFollowPlayer()
{
    camHolder.position = Vector3.SmoothDamp(transform.position, player.position + offset, 
                                             ref velocity, smoothSpeed * Time.deltaTime);
    transform.LookAt(player.position);
}

void RotateCamera()
{
    if(Input.GetKey (KeyCode.E))
    {
        rotateSpeed += 20f;
        if(rotateSpeed >= 20f)
        {
            rotateSpeed = 20f;
        transform.RotateAround(player.position, transform.up, rotateSpeed * Time.deltaTime);
        }
    }
    else
    {
        rotateSpeed = 0f;
    }

    if(Input.GetKey (KeyCode.Q))
    {
        rotateSpeed += 20f;
        if(rotateSpeed >= 20f)
        {
            rotateSpeed = 20f;
            transform.RotateAround(player.position, transform.up, -rotateSpeed * Time.deltaTime);
        }
    }
    else
    {
        rotateSpeed = 0f;
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You should not be rotating the same transform in both Update() and LateUpdate(), as they will conflict and you will see shaking / juddering, since you're telling it be one angle and then immediately you're telling it to be something else. You should probably(?) be managing the camHolder in one function, and the camera (which is this.transform) in the other function.

(I'm not really sure why you use Update() and LateUpdate() anyway, I would just use one.)

I'd start with this:

if (rotateSpeed == 0.0f)
   transform.LookAt(player.position);

This works fine for me:

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

public class CameraFollow : MonoBehaviour
{
    public Transform player;
    public float smoothSpeed = 0.125f;
    public float rotateSpeed;
    private Vector3 velocity = Vector3.zero;

    [SerializeField]
    private Vector3 offset;

    [Space]
    public Transform camHolder;

    void Update()
    {
        RotateCamera();

        CameraFollowPlayer();
    }

    void CameraFollowPlayer()
    {
        camHolder.position = Vector3.SmoothDamp(transform.position, player.position + offset, 
                                                 ref velocity, smoothSpeed * Time.deltaTime);
        //transform.LookAt(player.position);
    }

    void RotateCamera()
    {
        if(Input.GetKey (KeyCode.E))
        {
            rotateSpeed += 20f;
            if(rotateSpeed >= 20f)
            {
                rotateSpeed = 20f;
            transform.RotateAround(player.position, transform.up, rotateSpeed * Time.deltaTime);
            }
        }
        else
        {
            rotateSpeed = 0f;
        }

        if(Input.GetKey (KeyCode.Q))
        {
            rotateSpeed += 20f;
            if(rotateSpeed >= 20f)
            {
                rotateSpeed = 20f;
                transform.RotateAround(player.position, transform.up, -rotateSpeed * Time.deltaTime);
            }
        }
        else
        {
            rotateSpeed = 0f;
        }
    }
}
\$\endgroup\$
9
  • \$\begingroup\$ I moved CameraFollowPlayer() to Update function still shaking and I tried changing transform.LookAt(player.position); to camHolder.transform.LookAt(player.position); that just changes the whole view of the focus to the player and rotates verticaly that didnt worked either. Dont know what to do now.. \$\endgroup\$
    – Daniel
    Feb 20, 2018 at 7:52
  • \$\begingroup\$ @Daniel Can you explain to me how your scene is laid out? Camera is under camHolder... how is player related to these? \$\endgroup\$
    – Engineer
    Feb 20, 2018 at 7:53
  • \$\begingroup\$ The view should be third person, I have set an offset with which I had some problems too once because the camera wouldnt even rotate around the player on keypress it was like its locked when I would move just the camera itself around the player thats why I am moving the camHolder around the player that kinda did the rotation but now yes its shaking and there is no other relation to the player according to this infos. Okey no problem, write when you got time I try getting other things done, thanks. \$\endgroup\$
    – Daniel
    Feb 20, 2018 at 7:57
  • 1
    \$\begingroup\$ No they are seperate and my Character isnt a child of anything. \$\endgroup\$
    – Daniel
    Feb 20, 2018 at 8:00
  • 1
    \$\begingroup\$ Well I got it I watched a tutorial for Third Person Camera, but thank you! \$\endgroup\$
    – Daniel
    Feb 20, 2018 at 10:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .