Untitled

 avatar
unknown
plain_text
a month ago
1.0 kB
2
Indexable
public Transform player; // Reference to the player
public Vector3 offset = new Vector3(0, 5, -10); // Offset from the player
public float rotationSpeed = 100f; // Speed of camera rotation
public float smoothSpeed = 0.125f; // Smoothness of camera movement

private float yaw = 0f; // Yaw (horizontal) rotation

void LateUpdate()
{
    if (player == null)
    {
        Debug.LogWarning("Player is not assigned to the ThirdPersonCamera script.");
        return;
    }

    // Get input for rotation (optional for better control)
    yaw += Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;

    // Rotate the camera around the player
    Quaternion rotation = Quaternion.Euler(0, yaw, 0);

    // Calculate the desired position with the offset
    Vector3 desiredPosition = player.position + rotation * offset;

    // Smoothly move the camera to the desired position
    transform.position = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);

    // Always look at the player
    transform.LookAt(player.position);
}
Leave a Comment