Cinemachine
First-Person Controller
This example demonstrates how to implement a first-person controller in Unity using Cinemachine. It utilizes the Cinemachine Virtual Camera's 3rd Person Follow for a comprehensive experience. The script includes configurations for camera settings, mouse sensitivity, and managing player input for movement, rotation, crouching, and jumping.
Furthermore, the example seamlessly integrates events to handle camera transitions during crouch and uncrouch actions. Overall, it provides a solid foundation for a first-person perspective with Cinemachine integration, effectively managing both camera and player interactions.
In this setup, the Character will manage the yaw rotation (rotation along the character's up-axis), while the parented 'Camera Target' GameObject will handle the camera's pitch rotation.
public class FirstPersonController : MonoBehaviour
{
..
/// <summary>
/// Add input (affecting Yaw).
/// This is applied to the Character's rotation.
/// </summary>
public void AddControlYawInput(float value)
{
_character.AddYawInput(value);
}
/// <summary>
/// Add input (affecting Pitch).
/// This is applied to the cameraTarget's local rotation.
/// </summary>
public void AddControlPitchInput(float value, float minValue = -80.0f, float maxValue = 80.0f)
{
if (value == 0.0f)
return;
_cameraTargetPitch = MathLib.ClampAngle(_cameraTargetPitch + value, minValue, maxValue);
cameraTarget.transform.localRotation = Quaternion.Euler(-_cameraTargetPitch, 0.0f, 0.0f);
}
}It's important to disable the character's rotation mode since, in this example, we'll handle it ourselves.
Additionally, we'll use the Character Crouched and UnCrouched events to trigger a crouch/uncrouch animation, relying on Cinemachine's transitions.
Finally, we handle player input:
Third-Person Controller
The following example illustrates the implementation of a third-person controller in Unity using Cinemachine. It leverages the Cinemachine Virtual Camera's 3rd Person Follow feature for a comprehensive experience. The controller encompasses configurations for camera settings, mouse sensitivity, and the management of player input for movement, rotation, crouching, and jumping.
Initially, we implement methods to control the camera's rotation and adjust its follow distance:
Subsequently, we adjust both the rotation of the followTarget GameObject and the CameraDistance of the Cinemachine virtual camera. This essentially means that we are exerting control over the Cinemachine camera by manipulating the followTarget GameObject.
Finally, we handle player input:
Last updated