Following this tutorial, I created a third person camera to rotate around my character when the mouse is moved. The movement looks super jagged whenever the mouse is moved, and is only barely tolerable when I limit the movement with a “RotateSpeed” variable.
How should I go about smoothing the camera, and is my code below how it should be, or am I approaching it wrong?
Also, I am not currently using time.deltatime for camera movement. Should I? When I do, it transforms the camera right up against my player rather than keeping it where it was.
using UnityEngine;
using System.Collections;
public class CameraMovement : MonoBehaviour
{
public GameObject Target;
private float RotateSpeed = 1;
private Vector3 Offset;
private Quaternion HorizontalRotation;
private Quaternion VerticalRotation;
void Start()
{
Offset = Target.transform.position - transform.position;
HorizontalRotation = Quaternion.Euler(0, 0, 0);
VerticalRotation = Quaternion.Euler(0, 0, 0);
//TODO: Determine where this belongs.
//Cursor.lockState = CursorLockMode.Locked;
//Cursor.visible = false;
}
void LateUpdate()
{
MouseMovement();
}
void MouseMovement()
{
MouseHorizontal();
MouseVertical();
transform.LookAt(Target.transform);
transform.position = Target.transform.position - (HorizontalRotation * VerticalRotation * Offset * RotateSpeed);
}
void MouseHorizontal()
{
float horizontal = Input.GetAxis("Mouse X") * RotateSpeed;
Quaternion rotation = Quaternion.Euler(0, horizontal, 0);
HorizontalRotation = HorizontalRotation * rotation;
}
void MouseVertical()
{
float vertical = Input.GetAxis("Mouse Y") * RotateSpeed;
//flip vertical to make mouse up look up.
Quaternion rotation = Quaternion.Euler(-vertical, 0, 0);
VerticalRotation = VerticalRotation * rotation;
}
}
Thanks for any help!
–Mark