[英]Unity3D How to make character face secondary camera's direction when secondary camera is activated
[英]How to make character face direction of travel?
有谁知道我可以如何调整我的代码,使角色面向它行进的方向? 我意识到这个问题可能与现有问题重复,但我不确定在哪里/如何在我的代码中应用正确的轮换代码:
coroutineAllowed = false;
Vector2 p0 = routelist[routeIndex].routes[routeNumber].GetChild(0).position;
Vector2 p1 = routelist[routeIndex].routes[routeNumber].GetChild(1).position;
Vector2 p2 = routelist[routeIndex].routes[routeNumber].GetChild(2).position;
Vector2 p3 = routelist[routeIndex].routes[routeNumber].GetChild(3).position;
while (tParam < 1)
{
tParam += Time.deltaTime * speedModifier;
characterPosition = Mathf.Pow(1 - tParam, 3) * p0 +
3 * Mathf.Pow(1 - tParam, 2) * tParam * p1 +
3 * (1 - tParam) * Mathf.Pow(tParam, 2) * p2 +
Mathf.Pow(tParam, 3) * p3;
transform.position = characterPosition;
yield return new WaitForEndOfFrame();
}
forward
方向正常。 这就是为什么将它们的局部前向设置为与正交相机的方向正交往往会使它们不可见。从您在其他答案中共享的图像来看,您似乎需要将角色的局部“向上”方向旋转到他们移动的相反方向,同时保持角色的局部“向前”方向指向全局向前方向。
您可以使用Quaternion.LookRotation
来执行此操作,尽管您需要对参数有一点创意,将您想要远离的方向作为up
参数,将全局forward
作为向前参数。
coroutineAllowed = false;
Vector2 p0 = routelist[routeIndex].routes[routeNumber].GetChild(0).position;
Vector2 p1 = routelist[routeIndex].routes[routeNumber].GetChild(1).position;
Vector2 p2 = routelist[routeIndex].routes[routeNumber].GetChild(2).position;
Vector2 p3 = routelist[routeIndex].routes[routeNumber].GetChild(3).position;
while (tParam < 1)
{
tParam += Time.deltaTime * speedModifier;
Vector3 fromPosition = transform.position;
characterPosition = Mathf.Pow(1 - tParam, 3) * p0 +
3 * Mathf.Pow(1 - tParam, 2) * tParam * p1 +
3 * (1 - tParam) * Mathf.Pow(tParam, 2) * p2 +
Mathf.Pow(tParam, 3) * p3;
transform.position = characterPosition;
Vector3 newLocalUpDirection = fromPosition - transform.position;
transform.rotation = Quaternion.LookRotation(Vector3.forward, newLocalUpDirection);
yield return new WaitForEndOfFrame();
}
您可以像这样使用transform.LookAt(Vector3)
:
characterPosition = Mathf.Pow(1 - tParam, 3) * p0 +
3 * Mathf.Pow(1 - tParam, 2) * tParam * p1 +
3 * (1 - tParam) * Mathf.Pow(tParam, 2) * p2 +
Mathf.Pow(tParam, 3) * p3;
transform.LookAt(characterPosition);
transform.position = characterPosition;
尝试这样做,首先获取Lookvector
,然后引导角色查看它。
while (tParam < 1)
{
tParam += Time.deltaTime * speedModifier;
characterPosition = Mathf.Pow(1 - tParam, 3) * p0 +
3 * Mathf.Pow(1 - tParam, 2) * tParam * p1 +
3 * (1 - tParam) * Mathf.Pow(tParam, 2) * p2 +
Mathf.Pow(tParam, 3) * p3;
Vector3 v3 = characterPosition;
Vector3 Lookvector = v3 - transform.position;
Quaternion rotation = Quaternion.LookRotation(Lookvector);
transform.rotation = rotation;
transform.position = characterPosition;
yield return new WaitForEndOfFrame();
}
你有没有看过这个:
https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
这也可能在 x 和 z 空间中旋转,但您可以解决此问题并执行以下操作:
// Direction vector
Vector3 relativePos = target.position - transform.position;
// the second argument, upwards, defaults to Vector3.up
Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);
// fix rota so that only y rotation occures.
var vectorRota = rotation.EulerAngles;
Vector3 fixedYRotation = new Vector(0f, vectorRota.y, 0f);
// create Quaternion back from Vector3
Quaternion finalRotation = Quaternion.Euler(fixedYRotation );
// assing rotation to GameObject
gameObject.transform.rotation = finalRotation;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.