返回介绍

Vector3.Lerp 线性插值

发布于 2019-12-18 15:38:43 字数 2877 浏览 1023 评论 0 收藏 0

JavaScript => static function Lerp(from: Vector3, to: Vector3, t: float): Vector3;
C# => static Vector3 Lerp(Vector3 from, Vector3 to, float t);

Description 描述

Linearly interpolates between two vectors.

两个向量之间的线性插值。

Interpolates between from and to by the fraction t. This is most commonly used to find a point some fraction of the way along a line between two endpoints (eg, to move an object gradually between those points). This fraction is clamped to the range [0…1]. When t = 0 returns from. When t = 1 returns to. When t = 0.5 returns the point midway between from and to.

按照分数t在from到to之间插值。这是最常用的寻找一点沿一条线的两个端点之间一些分数的方式(例如,在那些点之间逐渐移动一个对象)。这分数是在范围[ 0…1]。t是夹在 [0…1]之间,当t = 0时,返回from,当t = 1时,返回to。当t = 0.5 返回from和to的中间点。

JavaScript:

	// Transforms to act as start and end markers for the journey.
	var startMarker: Transform;
	var endMarker: Transform;
 
	// Movement speed in units/sec.
	var speed = 1.0;
 
	// Time when the movement started.
	private var startTime: float;
 
	// Total distance between the markers.
	private var journeyLength: float;
 
	var target : Transform;
	var smooth = 5.0;
 
	function Start() {
		// Keep a note of the time the movement started.
		startTime = Time.time;
 
		// Calculate the journey length.
		journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
	}
 
	// Follows the target position like with a spring
	function Update () {
		// Distance moved = time * speed.
		var distCovered = (Time.time - startTime) * speed;
 
		// Fraction of journey completed = current distance divided by total distance.
		var fracJourney = distCovered / journeyLength;
 
		// Set our position as a fraction of the distance between the markers.
		transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public Transform startMarker;
    public Transform endMarker;
    public float speed = 1.0F;
    private float startTime;
    private float journeyLength;
    public Transform target;
    public float smooth = 5.0F;
    void Start() {
        startTime = Time.time;
        journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
    }
    void Update() {
        float distCovered = (Time.time - startTime) * speed;
        float fracJourney = distCovered / journeyLength;
        transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
    }
}

Vector3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文