如何使角色面向行进方向?

How to make character face direction of travel?

有谁知道我如何调整我的代码,使角色面向它行进的方向?我意识到这个问题似乎与现有问题重复,但我不确定 where/how 在我的代码中应用正确的旋转代码:

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();
}

您可以这样使用 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 方向旋转 space,但您可以解决此问题并执行以下操作:

// 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;

Unity 中的精灵(通常如果不总是)垂直于它们的局部 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();
}