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

Vector3.Lerp

Declaration

public static Vector3 Lerp(Vector3 a, Vector3 b, float t);

Параметры

Параметр Описание
a Начальное значение. Это значение возвращается, когда t = 0.
b Конечное значение. Это значение возвращается, когда t = 1.
t Значение, используемое для интерполяции между a и b. Значения, большие чем 1, закрепляются на 1. Значения, меньшие нуля, закрепляются на 0.

Возвращаемое значение

Vector3 Интерполированное значение. Это значение всегда лежит на линии между точками a и b.

Описание

Линейная интерполяция между двумя точками.

Параметр интерполяции t закрепляется в диапазоне [0, 1].

Этот метод полезен для нахождения точки на некоторой части пути вдоль линии между двумя конечными точками. Например, для постепенного перемещения объекта между этими точками.

Возвращаемое значение V равно
V = A + (BA) × t
где 0 < t < 1.

Метод интерполирует между точками a и bтаким образом, что:

  • Когда t ≤ 0, этот метод возвращает вектор a.
  • Когда 0 < t < 1, этот метод возвращает вектор, который указывает вдоль линии между a и b. Расстояние вдоль линии соответствует доле, представленной t.
  • Когда t > 1, этот метод возвращает вектор b.

// This example creates three primitive cubes. Using linear interpolation, one cube moves along the line between the others.
// Because the interpolation is clamped to the start and end points, the moving cube never passes the end cube, and remains at the end position after the interpolation frame limit is reached.    
// Attach this script to any GameObject in your scene. 

using UnityEngine;

public class LerpExample : MonoBehaviour { // Number of frames in which to completely interpolate between the positions int interpolationFramesCount = 300; int elapsedFrames = 0;

// Number of frames to reset the moving cube to the start position int maxFrameReset = 900;

GameObject CubeStart; GameObject CubeEnd; GameObject CubeMove;

void Start() { // Create the cubes CubeStart = GameObject.CreatePrimitive(PrimitiveType.Cube); CubeStart.transform.position = new Vector3(-5,0,0);

CubeEnd = GameObject.CreatePrimitive(PrimitiveType.Cube); CubeEnd.transform.position = new Vector3(5,0,0);

CubeMove = GameObject.CreatePrimitive(PrimitiveType.Cube); CubeMove.transform.position = CubeStart.transform.position; }

void Update() { float interpolationRatio = (float)elapsedFrames / interpolationFramesCount;

// Interpolate position of the moving cube, based on the ratio of elapsed frames CubeMove.transform.position = Vector3.Lerp(CubeStart.transform.position, CubeEnd.transform.position, interpolationRatio); // Reset elapsedFrames to zero after it reaches maxFrameReset elapsedFrames = (elapsedFrames + 1) % (maxFrameReset);

} }
// A longer example of Vector3.Lerp usage.
// Drop this script under an object in your scene, and specify 2 other objects in the "startMarker"/"endMarker" variables in the script inspector window.
// At play time, the script will move the object along a path between the position of those two markers.

using UnityEngine; using System.Collections;

public class ExampleClass : MonoBehaviour { // Transforms to act as start and end markers for the journey. public Transform startMarker; public Transform endMarker;

// Movement speed in units per second. public float speed = 1.0F;

// Time when the movement started. private float startTime;

// Total distance between the markers. private float journeyLength;

void Start() { // Keep a note of the time the movement started. startTime = Time.time;

// Calculate the journey length. journeyLength = Vector3.Distance(startMarker.position, endMarker.position); }

// Move to the target end position. void Update() { // Distance moved equals elapsed time times speed.. float distCovered = (Time.time - startTime) * speed;

// Fraction of journey completed equals current distance divided by total distance. float fractionOfJourney = distCovered / journeyLength;

// Set our position as a fraction of the distance between the markers. transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fractionOfJourney); } }

Дополнительные ресурсы: ,Slerp,, ,LerpUnclamped,.