目录
1.线性插值 之在规定时间内位移到指定位置
void Update()
{
//需求: 在2s内移动到指定位置
if (transform.position == Vector3.right * 10)
{
Debug.Log(Time.time);
}
else
{
//p [0-1]
var p = Time.time / sumTime;
// 具体公式: position =v1+(v2-v1)*p
transform.position = Vector3.Lerp(Vector3.zero, Vector3.right * 10,p );
}
}
2.球形插值 实现 声音pitch 由低变高 模拟汽车声浪
float sumTime = 10;
float lerpTime = 0f;
public AudioSource audioA;
private void Init()
{
audioA.pitch = 0.5f;
audioA.volume = 0.2f;
s_v3 = new Vector3(audioA.pitch, audioA.volume);
e_v3 = new Vector3(1.5f, 1f);
}
void Update()
{
lerpTime += Time.deltaTime;
var t_update = lerpTime / sumTime;
if (t_update >= 1)
{
lerpTime = 0;
is_up = !is_up;
}
if (is_up)
{
//球形插值 实现 声音pitch 由低变高
var newV = Vector3.Slerp(s_v3, e_v3, t_update);
audioA.pitch = newV.x;
audioA.volume = newV.y;
Debug.Log(newV.z);
}
else
{
//球形插值 实现 声音pitch 由低变高
var newV = Vector3.Slerp(e_v3, s_v3, t_update);
audioA.pitch = newV.x;
audioA.volume = newV.y;
}
}
3.附球形插值详解
原文地址:http://www.manew.com/thread-43314-1-1.html
防丢失kao'bei
Vector3.Slerp 球形插值详解
首先上官方的文档信息,方面还没有看过的同学学习。
static function Vector3 Slerp (Vector3 from, Vector3to, float t)
Description描述(翻译信息来自 U_鹰)
Spherically interpolates between two vectors.
球形插值在两个向量之间。我感觉叫“弧线插值”更直观一些。
Interpolates from towards to by amount t. The returned vector's magnitude will be interpolated between magnitudesof from and to.
通过t数值在from和to之间插值。返回的向量的长度将被插值到from到to的长度之间。
另外官方API上面还给了一个例子如下
[C#] 纯文本查看 复制代码
[/font][/size][/align][align=left][size=4][font=宋体] //在日出和日落之间动画弧线
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour
{
public Transform sunrise;
public Transform sunset;
void Update()
{
//弧线的中心
Vector3 center = sunrise.position + sunset.position * 0.5f;
//向下移动中心,垂直于弧线
center -= new Vector3(0, 1, 0);
//相对于中心在弧线上插值
Vector3 riseRelCe
|