I have a camera follow script and I am rotating the camera with Q and E key to right and left, the Camera. The Camera
is a child of a cam holdercamHolder
object. I comecame up with this code, itwhich rotates around the player to the right and left, but itsit's shaking as it tries to keep rotating around the player.
I didntdidn't come up with a solution to stop RotateAroundthe RotateAround
function, instead I tried it with setting rotateSpeedrotateSpeed
to 00
after I let go of the key, but didnt workedthat didn't work either.
Code:
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;
}
}