Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


SmoothDamp

Gradually changes a vector towards a desired goal over time.
Read time 3 minutesLast updated 4 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule

SmoothDamp(Vector3, Vector3, Vector3, float, float, float)

Gradually changes a vector towards a desired goal over time.
public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed, float deltaTime)

Parameters

current

The initial position.

target

The position to move towards.

currentVelocity

The initial velocity. This value is modified by the function each time it runs in the
Update
function. Pass this parameter as a reference value.

smoothTime

Approximately the time it will take to reach the target. A smaller value will reach the target faster.

maxSpeed

The maximum speed to reach in the motion. By default, there is no maximum speed.

deltaTime

The time between calls to this function. The default value is
Time.deltaTime
, such that
SmoothDamp
is called once per frame.

Returns

Type

Description

Vector3The new position, moved part of the way from
current
towards
target
.

Remarks

The vector is smoothed by a spring-like damper function, such that the speed slows as it nears the target position. The motion doesn't overshoot the target position.
A common use of this method is smoothing the motion of a follow camera.

Examples

// This example creates a sphere and moves the attached GameObject to // just in front of the sphere. // Attach this example to a camera object to view the movement.using UnityEngine;public class SmoothDampExample : MonoBehaviour{ public float smoothTime = 15; public Vector3 velocity = new Vector3(0,0,2); Vector3 targetPos; void Start() { // Position the camera transform.position = new Vector3(0,3,-10); // Create a sphere in front and below the camera. Vector3 spherePos = this.transform.position + new Vector3(0,-3,20); GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.position = spherePos; // Set final camera target position to just in front of the sphere targetPos = spherePos - new Vector3(0,0,2); } void Update() { // Smoothly move the camera towards that target position. The velocity // decreases as the camera moves closer to the target position transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref velocity, smoothTime); }}

SmoothDamp(Vector3, Vector3, Vector3, float, float, float)

Gradually changes a vector towards a desired goal over time.
public static Vector3 SmoothDamp(in Vector3 current, in Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed, float deltaTime)

Parameters

current

The initial position.

target

The position to move towards.

currentVelocity

The initial velocity. This value is modified by the function each time it runs in the
Update
function. Pass this parameter as a reference value.

smoothTime

Approximately the time it will take to reach the target. A smaller value will reach the target faster.

maxSpeed

The maximum speed to reach in the motion. By default, there is no maximum speed.

deltaTime

The time between calls to this function. The default value is
Time.deltaTime
, such that
SmoothDamp
is called once per frame.

Returns

Type

Description

Vector3The new position, moved part of the way from
current
towards
target
.

Remarks

The vector is smoothed by a spring-like damper function, such that the speed slows as it nears the target position. The motion doesn't overshoot the target position.
A common use of this method is smoothing the motion of a follow camera.

Examples

// This example creates a sphere and moves the attached GameObject to // just in front of the sphere. // Attach this example to a camera object to view the movement.using UnityEngine;public class SmoothDampExample : MonoBehaviour{ public float smoothTime = 15; public Vector3 velocity = new Vector3(0,0,2); Vector3 targetPos; void Start() { // Position the camera transform.position = new Vector3(0,3,-10); // Create a sphere in front and below the camera. Vector3 spherePos = this.transform.position + new Vector3(0,-3,20); GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.position = spherePos; // Set final camera target position to just in front of the sphere targetPos = spherePos - new Vector3(0,0,2); } void Update() { // Smoothly move the camera towards that target position. The velocity // decreases as the camera moves closer to the target position transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref velocity, smoothTime); }}