You get a bonus - 1 coin for daily activity. Now you have 1 coin

Unity Camera Shakes on Sudden Player Speed Changes: How to Fix

Lecture



To smooth out camera shake caused by sudden changes in movement speed in Unity, you can interpolate the camera position and/or apply a smoothing filter to make the camera motion gentler. Here are several approaches that can help:

1. Interpolating the camera position (Vector3.Lerp or Vector3.SmoothDamp)

Use Vector3.Lerp or Vector3.SmoothDamp to change the camera position smoothly when the player's position changes abruptly. For example:

using UnityEngine;
 public class SmoothCameraFollow : MonoBehaviour
 {
 public Transform player; // The player object public float smoothSpeed = 0.125f;
// The higher the value, the faster the camera reacts
 public Vector3 offset; // Offset from the player
 void LateUpdate()
{
Vector3 desiredPosition = player.position + offset;
 Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
 transform.position = smoothedPosition;
}
} 

Here smoothSpeed determines how smoothly the camera follows the player. Values between 0.1 and 0.2 usually give good results.

2. Using Rigidbody and the Interpolate option

If your player is controlled with a Rigidbody, make sure Interpolate is enabled for the camera and/or for the player's Rigidbody. This helps smooth out camera motion, especially during sudden changes of direction or speed.

  • Go to your player's Rigidbody component and set the Interpolate property to Interpolate.
  • You can also add a Rigidbody to the camera and enable Interpolate for it.

3. Using Cinemachine

The Cinemachine plugin (available in the Unity Asset Store) offers excellent features for smooth camera following. It provides options such as soft zone, dead zone, and rotation smoothing, which help avoid jerky motion.

An example Cinemachine setup:

  1. Install Cinemachine through the Unity Package Manager.
  2. Create a Cinemachine Virtual Camera and bind it to the player.
  3. Adjust the Damping parameters for position and rotation to smooth out camera motion during sudden speed changes.

Unity Camera Shakes on Sudden Player Speed Changes: How to Fix

4. Exponential Smoothing

For finer control over camera speed, you can use exponential smoothing:

void LateUpdate()
{
Vector3 targetPosition = player.position + offset;
float t = 1 - Mathf.Exp(-smoothSpeed * Time.deltaTime); // Exponential coefficient
transform.position = Vector3.Lerp(transform.position, targetPosition, t);
 } 

This smoothing is stronger for large speed changes and weaker for small ones.

Try combining these methods to find the degree of smoothness that suits your game.

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Software and information systems development"

Terms: Software and information systems development