Thirdperson camera rotation
Hi, The last project I worked on I used the following to handle the camera's rotation in first person.
void Look()
{
var target = _input.Look.ReadValue<Vector2>();
if (target.x > 0.01f
|| target.y > 0.01f
|| target.x < -0.01f
|| target.y < -0.01f)
{
var yaw = target.x * _mouseSensitivity * Time.deltaTime;
var pitch = target.y * _mouseSensitivity * Time.deltaTime;
_rotation -= pitch;
_rotation = Mathf.Clamp(_rotation, _minPitch, _maxPitch);
_camera.localRotation = Quaternion.Euler(_rotation, 0f, 0f);
transform.Rotate(Vector3.up * yaw);
}
}
This worked really nicely, so decided to take a similar approach for third person but hit a roadblock in not knowing how to calculate the rotation relative to a target. The best I can come up with is the following:
void Look()
{
var target = _input.Look.ReadValue<Vector2>();
if (target.x > 0.01f
|| target.y > 0.01f
|| target.x < -0.01f
|| target.y < -0.01f)
{
var yaw = target.x * _mouseSensitivity * Time.deltaTime;
var pitch = target.y * _mouseSensitivity * Time.deltaTime;
_rotation -= pitch;
_rotation = Mathf.Clamp(_rotation, _minPitch, _maxPitch);
if (_rotation > _minPitch && _rotation < _maxPitch)
_camera.RotateAround(transform.position, _camera.right, -pitch);
transform.Rotate(Vector3.up * yaw);
}
}
RotateAround() takes the incremental angle as opposed to hard setting the rotation as I did in my first person code. There are a few issues with the current code where for some reason the camera is allowed to rotate past the max pitch when jumping/moving/turning, this then strangely offsets to the point where it is almost top down.
Does anyone have any idea how I can do something more like: _camera.localRotation = Quaternion.Euler(_rotation, 0f, 0f);
but around a target?
Thanks