我正在尝试使鸟儿稍微“现实”地旋转,也就是说,矢量向上应该指向旋转的角度,我的意思是,如果将90º旋转到本地右侧,矢量向上现在应该指向-vector。向前(旋转之前的那个)。
我尝试过一个函数,但它有时会旋转,其余的只会覆盖vector.up,就好像它是0,1,0一样。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SmoothPath : MonoBehaviour {
[System.Serializable]
public class TrackingSettings
{
[HideInInspector]
public int currentPoint = 0;
[HideInInspector]
public int nextPoint = 1;
public int pathToFollow = 0;
public GameObject affectedObject;
public float speed = 10;
public float rotationSpeed = 12;
[HideInInspector]
public Quaternion lastRotation;
[HideInInspector]
public Vector3 lastLocation;
}
[System.Serializable]
public class PathsToFollow
{
public GameObject pathReference;
public Transform[] pathPoints;
}
public TrackingSettings[] objectsToTrack;
private TrackingSettings tempObjectToTrack;
private float timer = 0;
public PathsToFollow[] pathsToFollow;
private Quaternion lookRotation;
private Transform currentNode;
private Transform nextNode;
private float distance;
// Use this for initialization
void Start()
{
foreach (TrackingSettings ts in objectsToTrack)
{
ts.currentPoint = 0;
ts.nextPoint = 1;
ts.affectedObject.transform.position = pathsToFollow[ts.pathToFollow].pathPoints[0].position;
ts.lastRotation = ts.affectedObject.transform.rotation;
ts.lastLocation = ts.affectedObject.transform.position;
lookRotation = Quaternion.LookRotation(pathsToFollow[ts.pathToFollow].pathPoints[1].position - pathsToFollow[ts.pathToFollow].pathPoints[0].position);
ts.affectedObject.transform.rotation = lookRotation;
currentNode = pathsToFollow[ts.pathToFollow].pathPoints[ts.currentPoint];
nextNode = pathsToFollow[ts.pathToFollow].pathPoints[ts.nextPoint];
distance = Vector3.Distance(currentNode.position, nextNode.position);
}
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
for (int i = 0; i < objectsToTrack.Length; i++)
{
tempObjectToTrack = objectsToTrack[i];
if (Vector3.Distance(tempObjectToTrack.affectedObject.transform.position, nextNode.position) < 0.3f)
{
tempObjectToTrack.currentPoint = tempObjectToTrack.nextPoint;
tempObjectToTrack.nextPoint++;
//reset if overflow
if (tempObjectToTrack.nextPoint == pathsToFollow[tempObjectToTrack.pathToFollow].pathPoints.Length) tempObjectToTrack.nextPoint = 0;
timer = 0;
//lastNode = pathsToFollow[tempObjectToTrack.pathToFollow].pathPoints[tempObjectToTrack.lastPoint];
currentNode = pathsToFollow[tempObjectToTrack.pathToFollow].pathPoints[tempObjectToTrack.currentPoint];
nextNode = pathsToFollow[tempObjectToTrack.pathToFollow].pathPoints[tempObjectToTrack.nextPoint];
distance = Vector3.Distance(currentNode.position, nextNode.position);
float angle = GetAngle(tempObjectToTrack.affectedObject.transform.forward, Vector3.Normalize(nextNode.position - currentNode.position));
//set last rotation and location
tempObjectToTrack.lastRotation = tempObjectToTrack.affectedObject.transform.localRotation;
tempObjectToTrack.lastLocation = tempObjectToTrack.affectedObject.transform.position;
tempObjectToTrack.affectedObject.transform.up = new Vector3(angle / 2, 45, 0);
lookRotation = Quaternion.LookRotation(nextNode.position - tempObjectToTrack.affectedObject.transform.position, new Vector3(angle / 2, 45, 0));
Debug.Log("Up: " + new Vector3(angle / 2, 45, 0) + " - angle: " + angle);
//tempObjectToTrack.affectedObject.transform.localRotation = lookRotation;
}
tempObjectToTrack.affectedObject.transform.position = Vector3.SlerpUnclamped(tempObjectToTrack.lastLocation, nextNode.position, timer / distance * tempObjectToTrack.speed);
tempObjectToTrack.affectedObject.transform.rotation = Quaternion.Slerp (tempObjectToTrack.lastRotation, lookRotation, timer / distance * tempObjectToTrack.rotationSpeed);
}
}
private float GetAngle(Vector3 a, Vector3 b)
{
b.y = 0;
a.y = 0;
float angle = Vector3.Angle(a, b);
float sign = Mathf.Sign(Vector3.Dot(Vector3.up, Vector3.Cross(a, b)));
return angle * sign;
}
}
在圆形路径中(方形/圆形,有4个或更多航路点),这只鸟有时会正确指向,而另一些则不会。
答案 0 :(得分:0)
为此,我不会使用LookRotation
,因为您不知道希望本地up
完全是什么方向。这是一种直接使用角度更改横摇角度的方法:
首先,我们将GetAngle
的结果钳位到+/- 90f
。
float angle = GetAngle(tempObjectToTrack.affectedObject.transform.forward, Vector3.Normalize(nextNode.position - currentNode.position));
angle = Mathf.Clamp(angle,-90f,90f);
然后,我们将对象面向下一个目标,使局部up
为全局up
,然后通过将其乘以滚动旋转来将其滚动该数量:
lookRotation = Quaternion.LookRotation(nextNode.position - tempObjectToTrack.affectedObject.transform.position, Vector3.up);
// If the direction of roll isn't right, try `-1 * angle` here:
lookRotation = lookRotation * Quaternion.AngleAxis(angle, Vector3.forward);
然后,将Slerp
版的lookRotation
分配给rotation
:
tempObjectToTrack.affectedObject.transform.rotation = Quaternion.Slerp (tempObjectToTrack.lastRotation, lookRotation, timer / distance * tempObjectToTrack.rotationSpeed);