如果我理解正确,您只是想确保物体在飞行中在空中翻转给定的时间。你可以使用这样的东西:
using UnityEngine;
using System.Collections;
public class RotaionManager : MonoBehaviour {
// Motion start point
public Transform origin;
// Motion end point
public Transform destination;
// Start and en anle - 360 for single full rotation
public float startAngle = 0;
public float endAngle = 360;
void Update () {
// Distance to origin
var dstOrigin = Vector3.Distance(origin.position, transform.position);
// Distance to destination
var dstDestination = Vector3.Distance(destination.position, transform.position);
// Parameter
float t = (dstDestination + dstOrigin == 0 ? 0 : (dstOrigin / (dstDestination + dstOrigin) ) );
// The angle at current t
float angle = Mathf.Lerp(startAngle, endAngle, t);
// Rotate the object by the angle, then make sure it also faces destination
transform.rotation = Quaternion.LookRotation(destination.position - origin.position) * Quaternion.Euler(angle, 0, 0);
}
}
旋转角度的简单线性插值,基于到起点和终点的距离。您可以控制对象按开始和结束角度旋转的次数。观察旋转是为了除了绕自己的轴旋转外,对象还将面向目标的大致方向。
查看示例项目:
https://www.dropbox.com/s/g00srsjlqjx1jcz/RotationProj.zip?dl=0
编辑
在源-目的地向量上使用与对象投影的距离可能会更好,而不是原始距离,它在陡峭的路径上看起来更好。更新函数将如下所示:
void Update () {
// Projection of object on line between origin and destination
var projection = Vector3.Project(transform.position - origin.position, origin.position - destination.position) + origin.position;
// Distance to origin
var dstOrigin = Vector3.Distance(origin.position, projection);
// Distance to destination
var dstDestination = Vector3.Distance(destination.position, projection);
// Parameter
float t = (dstDestination + dstOrigin == 0 ? 0 : (dstOrigin / (dstDestination + dstOrigin) ) );
// The angle at current t
float angle = Mathf.Lerp(startAngle, endAngle, t);
// Rotate the object by the angle, then make sure it also faces destination
transform.rotation = Quaternion.LookRotation(destination.position - origin.position) * Quaternion.Euler(angle, 0, 0);
}
我还更新了示例项目
编辑
另一种看起来更好的方法是忽略起点和终点的Y坐标,并进行投影。在某些极端情况下看起来要好一些。代码如下所示:
void Update () {
Vector3 op = origin.position; op.y = 0;
Vector3 dp = destination.position; dp.y = 0;
// Projection of object on line between origin and destination
var projection = Vector3.Project(transform.position - origin.position, op - dp) + op;
// Distance to origin
var dstOrigin = Vector3.Distance(op, projection);
// Distance to destination
var dstDestination = Vector3.Distance(dp, projection);
// Parameter
float t = (dstDestination + dstOrigin == 0 ? 0 : (dstOrigin / (dstDestination + dstOrigin) ) );
// The angle at current t
float angle = Mathf.Lerp(startAngle, endAngle, t);
// Rotate the object by the angle, then make sure it also faces destination
transform.rotation = Quaternion.LookRotation(dp - op) * Quaternion.Euler(angle, 0, 0);
}