The Mathf.SmoothDamp function in Unity3D is used with Mathf mathematical operations (C#)

Unity Mathf Math (C#)

Today, I started to look at the Metal Slug example, and noticed the use of this function Mathf.SmoothDamp, which is used in the game to do buffer tracking for cameras and tracking soldiers for boss helicopters. This function is one of the Mathf mathematical operation functions in Unity3D. We will actually pay attention to some games that use the function of smoothmove, which is actually a similar effect, but found that this function has been easily packaged. I checked the official website documentation and found that it is really very simple to use.

smoothdamp, my understanding is a smooth buffer, something is not a rigid movement but a deceleration buffer movement to a specified position. Let’s look at the code:

public 
         Transform target;        
         //The player


             
         public 
         float 
         smoothTime= 0.3f;  
         //Smooth Time


             
         private 
         Vector2 velocity;       
         //Velocity


             
         void  
         Update ()


             
         {


                 
         //Set the position


                 
         transform.position = 
         new 
         Vector3(Mathf.SmoothDamp(transform.position.x, target.position.x, 
         ref 
         velocity.x, smoothTime),Mathf.SmoothDamp( transform.position.y, target.position.y, 
         ref 
         velocity.y, smoothTime),transform.position.z);


             
         }

Here is a little explanation, target (tracked target), smoothtime (buffering time, the longer the time, the slower the buffering speed and the slower the movement), velocity (relative buffering deceleration, it looks very complicated and complicated, in fact we don’t need it at all Ignore him, it is only used as a bearing variable, we just need to assign the default value to 0.)

In the update, the object (actually the camera) is moved to the x-axis and y-axis, because it is a horizontal version of the game~, you can actually specify the velocity as a float value and use it directly.

Mathf.Abs absolute value

Calculates and returns the absolute value of the specified parameter f.

Mathf.Acos arc cosine

static function Acos (f : float) : float

Calculates and returns the arc cosine of the number specified in parameter f in radians.

Mathf.Approximately approximate

static function Approximately (a : float, b: float) : bool

Compares two floating-point values ​​to see if they are very close, since floating-point values ​​are imprecise, using equals to compare them is not recommended. For example, 1.0==10.0/10.0 might not return true.

public class example : MonoBehaviour {

publicvoid Awake() {

if(Mathf.Approximately(1.0F, 10.0F / 10.0F))

print(“same”);

}

}

Mathf.Asin arcsine

static function Asin (f : float) : float

Calculates and returns the arcsine of the number specified in parameter f in radians.

Mathf.Atan2 arctangent

static function Atan2 (y : float, x :float) : float

Calculates and returns the arctangent of y/x in radians. The return value represents the angle relative to the opposite corner of a right triangle, where x is the length of the limb and y is the length of the opposite side.

The return value is the angle between the x-axis and a 2D vector starting at 0 and ending at (x,y).

public class example : MonoBehaviour {

publicTransform target;

voidUpdate() {

Vector3relative = transform.InverseTransformPoint(target.position);

floatangle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg;

transform.Rotate(0,angle, 0);

}

}

Mathf.Atan arc tangent

static function Atan (f : float) :float

Calculates and returns the arctangent of the number specified in parameter f. The return value is between negative half pi and positive half pi.

Mathf.CeilToInt smallest integer

static function CeilToInt (f : float) : int

Returns the smallest integer greater than or equal to f.

Mathf.Ceil upper limit value

static function Ceil (f : float) : float

Returns the upper limit of the number or expression specified by f. The upper limit of a number is the nearest whole number greater than or equal to the number.

Mathf.Clamp01 limit 0~1

static function Clamp01 (value : float) :float

Limit value to 0,1 and return value. Returns 0 if value is less than 0. If value is greater than 1, return 1, otherwise return value.

Mathf.Clamp Limits

static function Clamp (value : float, min :float, max : float) : float

Limit the value of value between min and max, and return min if value is less than min. If value is greater than max, return max, otherwise return value

static function Clamp (value : int, min :int, max : int) : int

Limit the value of value between min and max, and return value.

Mathf.ClosestPowerOfTwo nearest quadratic

static function ClosestPowerOfTwo (value :int) : int

Returns the nearest power of 2 to value.

Mathf.Cos cosine

static function Cos (f : float) : float

Returns the cosine of the angle specified by parameter f (a value between -1.0 and 1.0).

Mathf.Deg2Rad degrees to radians

static var Deg2Rad : float

Degree to radian conversion constant. (read only)

This equals (PI * 2) / 360.

Mathf.Mathf.Rad2Deg radian rotation

static var Rad2Deg : float

Conversion constant from radians to degrees. (read only)

This equals 360 / (PI * 2).

Mathf.DeltaAngle Incremental angle

static function DeltaAngle (current :float, target : float) : float

Calculates the shortest difference between two given corners.

// Prints 90

Debug.Log(Mathf.DeltaAngle(1080,90));

Mathf.Epsilon small positive number

static var Epsilon : float

A small floating-point value. (read only)

Smallest floating-point value, other than 0.

The following rules:

–    anyValue + Epsilon = anyValue

–    anyValue – Epsilon = anyValue

–    0 + Epsilon = Epsilon

–    0 – Epsilon = -Epsilon

A value between Arbitrary and Epsilon will cause a truncation error at Arbitrary.

public class example : MonoBehaviour {

boolisEqual(float a, float b) {

if(a>= b – Mathf.Epsilon && a <= b + Mathf.Epsilon)

returntrue;

else

return false;

}

}

Mathf.Exp exponent

static function Exp (power : float) : float

Returns the value of e raised to the power.

Mathf.FloorToInt largest integer

static function FloorToInt (f : float) :int

Returns the largest integer less than or equal to f.

Mathf.Floor lower limit

static function Floor (f : float) : float

Returns the lower limit of the number or expression specified in parameter f. The lower limit is the nearest integer less than or equal to the specified number or expression.

Mathf.Infinity positive infinity

static var Infinity : float

Represents positive infinity, which is infinity, ∞ (read-only)

Mathf.InverseLerp inverse interpolation

Calculates the Lerp parameter between two values. That is, the ratio of value between from and to.

// now the parameter is 3/5

float parameter =Mathf.InverseLerp(walkSpeed, runSpeed, speed);

Is Mathf.IsPowerOfTwo a power of 2

static function IsPowerOfTwo (value : int): bool

Returns true if the value is a power of 2.

// prints false

Debug.Log(Mathf.IsPowerOfTwo(7));

// prints true

Debug.Log(Mathf.IsPowerOfTwo(32));

Mathf.LerpAngle interpolation angle

static function LerpAngle (a : float, b :float, t : float) : float

Same as Lerp’s principle, when they wrap 360 degrees make sure the interpolation is correct.

a and b represent degrees.

public class example : MonoBehaviour {

publicfloat minAngle = 0.0F;

publicfloat maxAngle = 90.0F;

voidUpdate() {

floatangle = Mathf.LerpAngle(minAngle, maxAngle, Time.time);

transform.eulerAngles= new Vector3(0, angle, 0);

}

}

Mathf.Lerp interpolation

static function Lerp (from : float, to :float, t : float) : float

Returns the interpolated value between a and b based on a floating-point number t, constrained between 0 and 1.

Returns from when t = 0 and to when t = 1. Returns the mean of from and to when t = 0.5.

Mathf.Log10 base 10 logarithm

static function Log10 (f : float) : float

Returns the logarithm of f, base 10.

Mathf.Log logarithm

static function Log (f : float, p : float): float

Returns the logarithm of the argument f.

// logarithm of 6 in base 2

// base 2 logarithm of 6

// prints 2.584963

print(Mathf.Log(6, 2));

Mathf.Max maximum value

static function Max (a : float, b : float): float

static function Max (params values :float[]) : float

Returns the largest of two or more values.

Mathf.Min Min

static function Min (a : float, b : float): float

static function Min (params values :float[]) : float

Returns the smallest of two or more values.

Mathf.MoveTowardsAngle move angle

static function MoveTowardsAngle (current :float, target : float, maxDelta : float) : float

Like MoveTowards, but when they wrap 360 degrees make sure the interpolation is correct.

The variables current and target are given as degrees. For optimization reasons, negative values ​​of maxDelta are not supported and may cause oscillations. Push current away from the target angle and add a 180 degree angle instead.

Mathf.MoveTowards

static function MoveTowards (current :float, target : float, maxDelta : float) : float

Change a current value closer to the target value.

This is effectively the same as Mathf.Lerp, but this function will ensure that our velocity does not exceed maxDelta. A negative maxDelta value pushes the target away from it.

Mathf.NegativeInfinity negative infinity

static var NegativeInfinity : float

Represents negative infinity, which is infinitesimal, -∞ (read-only)

Mathf.NextPowerOfTwo next power of 2

Mathf.PingPong

static function PingPong (t : float, length: float) : float

Round trip between 0 and length. The value of t is never greater than the value of length and never less than 0.

The returned value will move back and forthbetween 0 and length.

The return value will move back and forth between 0 and length.

Mathf.PI Pi

static var PI : float

The value of PI (read pai), which is the value of pi (π) 3.14159265358979323846… (read only) 

Mathf.Pow power

static function Pow (f : float, p : float): float

Computes and returns f raised to the p power.

Mathf.Repeat

static function Repeat (t : float, length :float) : float

Loop the value t, between 0 and length. The value of t is never greater than the value of length and never less than 0.

This is similar to the modulo operator, but can use floating point numbers.

public class example : MonoBehaviour {

voidUpdate() {

transform.position= new Vector3(Mathf.Repeat(Time.time, 3), transform.position.y,transform.position.z);

}

}

Mathf.RoundToInt rounds to an integer

static function RoundToInt (f : float) :int

Returns the value specified by f rounded to the nearest integer.

If the number ends in .5, so it is in the middle of two integers, even or odd, an even number will be returned.

Mathf.Round rounding

static function Round (f : float) : float

Returns the floating point number f rounded to the nearest integer.

If the number ends in .5, so it is in the middle of two integers, even or odd, an even number will be returned.

Mathf.Sign notation

static function Sign (f : float) : float

Returns the sign of f.

Returns 1 when f is positive or 0, and -1 when f is negative.

Mathf.Sin Sine

static function Sin (f : float) : float

Calculates and returns the sine of the specified angle f in radians.

Mathf.SmoothDampAngle smooth damping angle

static function SmoothDampAngle (current :float, target : float, ref currentVelocity : float, smoothTime : float,maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float 

parameter

current

current location.

target

The position we are trying to reach.

currentVelocity

The current speed, this value will be modified at any time when you access this function.

smoothTime

the target faster.

Approximate time to reach the target position, the actual time to reach the target is faster.

maxSpeed

Optional parameter that allows you to limit the maximum speed.

deltaTime

The time since the last time the function was called. The default is Time.deltaTime.

Gradually change a given angle to the desired angle over time.

This value is smoothed by some spring damper-like functions. This function can be used to smooth any kind of value, position, color, scalar. The most common is smoothing a following camera.

// A simple smooth follow camera

// follow the direction of the target

public class example : MonoBehaviour {

publicTransform target;

publicfloat smooth = 0.3F;

publicfloat distance = 5.0F;

privatefloat yVelocity = 0.0F;

voidUpdate() {

// Transform from the current y angle to the target y angle

floatyAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target.eulerAngles.y,ref yVelocity, smooth);

//target position

Vector3position = target.position;

// Then, the distance offset after the new angle

position+= Quaternion.Euler(0, yAngle, 0) * new Vector3(0, 0, -distance);

// apply location

transform.position= position;

// look at the target

transform.LookAt(target);

}

}

Mathf.SmoothDamp smooth damping

static function SmoothDamp (current :float, target : float, ref currentVelocity : float, smoothTime : float,maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : float

parameter

current

current location.

target

The position we are trying to reach.

currentVelocity

The current speed, this value will be modified at any time when you access this function.

smoothTime

Approximate time to reach the target position, the actual time to reach the target is faster.

maxSpeed

Optional parameter that allows you to limit the maximum speed.

deltaTime

The time since the last time the function was called. The default is Time.deltaTime.

describe

Gradually change a value over time to the desired value.

This value is smoothed like a spring damper that doesn’t collapse. This function can be used to smooth any type of value, position, color, scalar.

public class example : MonoBehaviour {

publicTransform target;

publicfloat smoothTime = 0.3F;

privatefloat yVelocity = 0.0F;

voidUpdate() {

floatnewPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, refyVelocity, smoothTime);

transform.position= new Vector3(transform.position.x, newPosition, transform.position.z);

}

}

Mathf.SmoothStep smooth interpolation

static function SmoothStep (from : float,to : float, t : float) : float

Similar to lerp, interpolates between min and max, and fades in and out at limits.

public class example : MonoBehaviour {

publicfloat minimum = 10.0F;

publicfloat maximum = 20.0F;

voidUpdate() {

transform.position= new Vector3(Mathf.SmoothStep(minimum, maximum, Time.time), 0, 0);

}

}

Mathf.Sqrt square root

static function Sqrt (f : float) : float

Computes and returns the square root of f.

static function Tan (f : float) : float

Calculates and returns the tangent of the specified angle f in radians.

Leave a Comment

Your email address will not be published. Required fields are marked *