Unity 6.3
0 онлайн 81 гостей 3 в системе
Вход

Vector3.SmoothDamp

Declaration

public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed = Mathf.Infinity, float deltaTime = Time.deltaTime);

Параметры

Параметр Описание
текущий Начальная позиция.
Цель Позиция, к которой следует двигаться.
currentVelocity Начальная скорость. Это значение изменяется функцией каждый раз, когда она запускается в функции Update. Передайте этот параметр в качестве справочного значения.
smoothTime Примерное время, которое потребуется для достижения цели. Меньшее значение означает, что цель будет достигнута быстрее.
maxSpeed Максимальная скорость, которую нужно достичь в движении. По умолчанию максимальная скорость не указывается.
deltaTime Время между вызовами этой функции. Значение по умолчанию — Time.deltaTime, так что SmoothDamp вызывается один раз в кадре.

Описание

Постепенное изменение вектора в направлении желаемой цели с течением времени.

Вектор сглаживается функцией амортизатора, подобной пружине, так что скорость замедляется по мере приближения к целевой позиции. Движение не превышает целевой позиции.

Общим применением этого метода является сглаживание движения камеры следования.

// 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); } }