如何记录 NavMesh AI 代理的方向
How to Record Direction of NavMesh AI agents
我想知道如何获取 Navmesh Agent 行走的方向,使用这些值我计划将它们引用到 Blendtree 以根据 AI 方向播放某些动画,使用 2D自由形式的笛卡尔平面我计划使用这些参考方向值来播放某些动画,X 和 Z 方向都从零开始。所以向前走 Z 值将是 1,而 X 值将是 0
目前我已经尝试使用刚体速度和 transform.forward(X 和 Z),虽然我没有收到任何使用速度的值,但 transform.forward 确实给出了增加的值,但它看起来不像我正在寻找的值(这些值需要在 0-1 的范围内),以及从来没有 0,0
的起始位置(空闲)
void animationUpdate() {
anim.SetFloat("Enemy Z", /* Unsure what to do*/);
anim.SetFloat("Enemy X", /* Unsure what to do*/);
if (/*transform.forward.x != 0 && transform.forward.z != 0*/) {
anim.SetBool("isWalking", true);
} else {
anim.SetBool("isWalking", false);
}
}
我的笛卡尔平面如下
如有任何帮助,我们将不胜感激,
感谢阅读
您可以使用 desiredVelocity
找到它的移动方向,然后 Vector3.Project
将其分解为它的移动方式 forward/right。
Vector3 normalizedMovement = nav.desiredVelocity.normalized;
Vector3 forwardVector = Vector3.Project(normalizedMovement, transform.forward);
Vector3 rightVector = Vector3.Project (normalizedMovement, transform.right);
获取幅度(因为我们预测了标准化运动,它将是 -1 和 1 之间的浮点数)并使用 Vector3.Dot
确定每个分量中幅度的符号:
// Dot(direction1, direction2) = 1 if they are in the same direction, -1 if they are opposite
float forwardVelocity = forwardVector.magnitude * Vector3.Dot(forwardVector, transform.forward);
float rightVelocity = rightVector.magnitude * Vector3.Dot(rightVector, transform.right);
现在,您可以将它们分配给您的动画,使用 Mathf.InverseLerp
:
将范围从 [-1,1] 转换为 [0,1]
anim.SetFloat("Enemy Z", Mathf.InverseLerp(-1f, 1f, forwardVelocity));
anim.SetFloat("Enemy X", Mathf.InverseLerp(-1f, 1f, rightVelocity));
我想知道如何获取 Navmesh Agent 行走的方向,使用这些值我计划将它们引用到 Blendtree 以根据 AI 方向播放某些动画,使用 2D自由形式的笛卡尔平面我计划使用这些参考方向值来播放某些动画,X 和 Z 方向都从零开始。所以向前走 Z 值将是 1,而 X 值将是 0
目前我已经尝试使用刚体速度和 transform.forward(X 和 Z),虽然我没有收到任何使用速度的值,但 transform.forward 确实给出了增加的值,但它看起来不像我正在寻找的值(这些值需要在 0-1 的范围内),以及从来没有 0,0
的起始位置(空闲)void animationUpdate() {
anim.SetFloat("Enemy Z", /* Unsure what to do*/);
anim.SetFloat("Enemy X", /* Unsure what to do*/);
if (/*transform.forward.x != 0 && transform.forward.z != 0*/) {
anim.SetBool("isWalking", true);
} else {
anim.SetBool("isWalking", false);
}
}
我的笛卡尔平面如下
如有任何帮助,我们将不胜感激, 感谢阅读
您可以使用 desiredVelocity
找到它的移动方向,然后 Vector3.Project
将其分解为它的移动方式 forward/right。
Vector3 normalizedMovement = nav.desiredVelocity.normalized;
Vector3 forwardVector = Vector3.Project(normalizedMovement, transform.forward);
Vector3 rightVector = Vector3.Project (normalizedMovement, transform.right);
获取幅度(因为我们预测了标准化运动,它将是 -1 和 1 之间的浮点数)并使用 Vector3.Dot
确定每个分量中幅度的符号:
// Dot(direction1, direction2) = 1 if they are in the same direction, -1 if they are opposite
float forwardVelocity = forwardVector.magnitude * Vector3.Dot(forwardVector, transform.forward);
float rightVelocity = rightVector.magnitude * Vector3.Dot(rightVector, transform.right);
现在,您可以将它们分配给您的动画,使用 Mathf.InverseLerp
:
anim.SetFloat("Enemy Z", Mathf.InverseLerp(-1f, 1f, forwardVelocity));
anim.SetFloat("Enemy X", Mathf.InverseLerp(-1f, 1f, rightVelocity));