[英]Unity Fix NavMeshAgent Auto Rotate 90 degrees
我目前正在使用出租車模型作為 AI 的敵人,它將駕駛到不同的航點。 每當汽車移動到一個航點時,它會立即自動向右旋轉 90 度,但會繼續從一個航點移動到另一個航點。
如何修復移動到航點時自動旋轉 90 度的 NavMeshAgent? 注釋掉的代碼修復了自動旋轉,但在 setDestination 上移動到航點時旋轉不夠。
未注釋的代碼首先旋轉 90 度,然后在每個航點之后旋轉一點(從 90 度位置)。 (來自 Vector3.RotateTowards 的 Unity API 腳本)
_agent.UpdateRotation = false ,停止初始旋轉,但我必須手動控制旋轉(我很難使用)
private void Start()
{
_agent = GetComponent<NavMeshAgent>();
// Almost works doesn't rotate enough
//_agent.updateRotation = false;
_isStopped = false;
}
private void Update()
{
//Almost works, doesn't rotate enough
//transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, (_angleToRotate) * 8, 0), 1f);
//Rotates but turns 90 degrees first
Vector3 targetDirection = _wayPoints[_currentWayPoint].transform.position - transform.position;
float singleStep = _speed * Time.deltaTime;
Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f);
Debug.DrawRay(transform.position, newDirection, Color.red);
transform.rotation = Quaternion.LookRotation(newDirection);
switch (_currentState)
{
case AIState.NonAlert:
//Debug.Log("Not Alert...");
if (_isStopped == true)
{
return;
}
else
{
if (_wayPoints.Count > 0)
{
_agent.SetDestination(_wayPoints[_currentWayPoint].transform.position);
//Gets distance between two Vector3s
float distanceToWayPoint = Vector3.Distance(_wayPoints[_currentWayPoint].transform.position, transform.position);
if (distanceToWayPoint < 1.0f)
{
_currentWayPoint++;
//Almost works, doesnt rotate enough
//_angleToRotate = Vector3.SignedAngle(transform.position, _wayPoints[_currentWayPoint].transform.position, Vector3.up);
}
}
}
這是因為出租車模型本身具有 90° 偏移。 您可以通過為出租車模型創建一個預制件來改變它,將一個空游戲對象作為父項,並在預制件上為出租車提供所需的 90° 偏移。 之后,您使用父對象進行移動和旋轉
或者
在攪拌機或任何其他 3d 建模工具中更改模型的方向
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.