To apply smooth rotation around an axis to a camera control script for orbiting around a target point, you can adapt the previous smooth rotation methods to control the camera’s position and orientation relative to the target. This will allow your camera to orbit smoothly around the target point, providing a natural and responsive feel, especially when controlled by user input like mouse movement.
- Target Transform: The object or point around which the camera will orbit.
- Distance: How far the camera should be from the target.
- Rotation Angles: The horizontal (yaw) and vertical (pitch) angles determining the camera’s orientation.
- Smoothing: Use
Mathf.SmoothDampAngle to smoothly interpolate the rotation angles.
- User Input: Capture and process user input to control the desired angles.
1. Define the Camera’s Orbit Parameters
Create variables to store the target, distance, rotation speeds, smoothing time, and angle limits.
public Transform target; // The target to orbit around
public float distance = 5.0f; // Distance from the target
public float xSpeed = 120.0f; // Horizontal rotation speed
public float ySpeed = 120.0f; // Vertical rotation speed
public float smoothTime = 0.3f; // Smoothing time
public float yMinLimit = -20f; // Minimum vertical angle
public float yMaxLimit = 80f; // Maximum vertical angle
2. Initialize Rotation Angles and Velocities
Set up variables to keep track of the desired and current angles, as well as their velocities for smoothing.
private float desiredYaw = 0.0f;
private float desiredPitch = 0.0f;
private float currentYaw = 0.0f;
private float currentPitch = 0.0f;
private float yawVelocity = 0.0f;
private float pitchVelocity = 0.0f;
3. Capture and Process User Input
In the LateUpdate() method, get user input and adjust the desired angles accordingly.
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
// Adjust desired angles based on input
desiredYaw += mouseX * xSpeed * Time.deltaTime;
desiredPitch -= mouseY * ySpeed * Time.deltaTime;
// Clamp the vertical angle to prevent flipping
desiredPitch = Mathf.Clamp(desiredPitch, yMinLimit, yMaxLimit);
// Smoothly interpolate current angles towards desired angles
currentYaw = Mathf.SmoothDampAngle(currentYaw, desiredYaw, ref yawVelocity, smoothTime);
currentPitch = Mathf.SmoothDampAngle(currentPitch, desiredPitch, ref pitchVelocity, smoothTime);
// Update camera's rotation and position
4. Calculate and Apply the Smoothed Rotation
Create a method to update the camera’s position and rotation based on the smoothed angles.
void UpdateCameraPosition()
// Create rotation from the current angles
Quaternion rotation = Quaternion.Euler(currentPitch, currentYaw, 0);
// Calculate the desired position
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
// Apply rotation and position to the camera
transform.rotation = rotation;
transform.position = position;
5. Lock and Hide the Cursor (Optional)
For a better user experience, you might want to lock and hide the cursor during camera control.
// Initialize angles based on the current rotation
Vector3 angles = transform.eulerAngles;
desiredYaw = currentYaw = angles.y;
desiredPitch = currentPitch = angles.x;
// Lock and hide the cursor
Cursor.lockState = CursorLockMode.Locked;
public class SmoothOrbitCamera : MonoBehaviour
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float smoothTime = 0.3f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
private float desiredYaw = 0.0f;
private float desiredPitch = 0.0f;
private float currentYaw = 0.0f;
private float currentPitch = 0.0f;
private float yawVelocity = 0.0f;
private float pitchVelocity = 0.0f;
Vector3 angles = transform.eulerAngles;
desiredYaw = currentYaw = angles.y;
desiredPitch = currentPitch = angles.x;
// Lock and hide the cursor
Cursor.lockState = CursorLockMode.Locked;
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
// Adjust desired angles based on input
desiredYaw += mouseX * xSpeed * Time.deltaTime;
desiredPitch -= mouseY * ySpeed * Time.deltaTime;
// Clamp the vertical angle
desiredPitch = Mathf.Clamp(desiredPitch, yMinLimit, yMaxLimit);
// Smoothly interpolate the current angles
currentYaw = Mathf.SmoothDampAngle(currentYaw, desiredYaw, ref yawVelocity, smoothTime);
currentPitch = Mathf.SmoothDampAngle(currentPitch, desiredPitch, ref pitchVelocity, smoothTime);
// Update camera's rotation and position
void UpdateCameraPosition()
// Create rotation from the current angles
Quaternion rotation = Quaternion.Euler(currentPitch, currentYaw, 0);
// Calculate the desired position
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
// Apply rotation and position to the camera
transform.rotation = rotation;
transform.position = position;
-
User Input: The script captures mouse movement to control the camera’s orbit around the target.
mouseX affects the horizontal rotation (desiredYaw).
mouseY affects the vertical rotation (desiredPitch).
-
Angle Clamping: The vertical angle is clamped between yMinLimit and yMaxLimit to prevent the camera from flipping over the top or bottom of the target.
-
Smoothing with Mathf.SmoothDampAngle:
- Provides smooth transitions between angles, avoiding abrupt changes.
- Uses
ref parameters (yawVelocity and pitchVelocity) to keep track of the velocity required for smoothing.
-
Camera Position and Rotation:
- The camera’s position is calculated based on the smoothed angles, ensuring it maintains the specified
distance from the target.
- The camera’s rotation is set to look at the target from the calculated position.
-
LateUpdate Method:
- Using
LateUpdate() ensures that camera movement happens after all other updates, preventing jitter when following moving targets.
1. Zoom Functionality
Add the ability to zoom in and out with the mouse scroll wheel.
public float zoomSpeed = 2.0f;
public float minDistance = 2.0f;
public float maxDistance = 15.0f;
// Existing input handling...
float scroll = Input.GetAxis("Mouse ScrollWheel");
distance -= scroll * zoomSpeed;
distance = Mathf.Clamp(distance, minDistance, maxDistance);
// Existing smoothing and camera update...
2. Adjusting Sensitivity
If the camera is rotating too quickly or slowly, adjust xSpeed and ySpeed to change the rotation sensitivity.
3. Touch Input Support
For mobile devices, replace mouse input with touch input.
if (Input.touchCount == 1)
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
desiredYaw += touch.deltaPosition.x * xSpeed * Time.deltaTime * 0.1f;
desiredPitch -= touch.deltaPosition.y * ySpeed * Time.deltaTime * 0.1f;
4. Keyboard Input Support
Add keyboard controls for rotation.
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
desiredYaw += horizontalInput * xSpeed * Time.deltaTime;
desiredPitch -= verticalInput * ySpeed * Time.deltaTime;
5. Reset Camera Position
Implement a method to reset the camera to its default position.
public void ResetCamera()
desiredYaw = currentYaw = 0.0f;
desiredPitch = currentPitch = 0.0f;
6. Smooth Time Adjustment
Allow dynamic adjustment of smoothTime to control how quickly the camera responds.
-
Performance: While this script is efficient for most applications, ensure you’re not calling unnecessary functions or computations within LateUpdate().
-
Camera Collision: If needed, implement collision detection to prevent the camera from clipping through objects.
-
Testing: Test the camera controls with different target positions and movement speeds to ensure smooth operation.
-
User Experience: Make sure the camera movement feels natural and responsive. Adjust parameters as needed based on user feedback.
-
Gimbal Lock Avoidance: By using quaternions and clamping the vertical angle, you prevent gimbal lock issues.
By adapting the smooth rotation techniques to your camera control script, you can achieve smooth and intuitive orbiting around a target point. The use of Mathf.SmoothDampAngle ensures that rotations are smoothly interpolated, providing a polished feel to camera movements in your Unity project.
Remember to adjust the parameters like xSpeed, ySpeed, smoothTime, and angle limits to suit your specific requirements. Testing and refining these values will help you create the best user experience.
Feel free to ask if you need further customization or assistance with specific features!