【问题标题】:Unity NavMeshAgent not reaching target - moving aroundUnity NavMeshAgent 未达到目标 - 移动
【发布时间】:2019-05-31 23:40:52
【问题描述】:

我已经创建了一个简单的场景,现在我正在尝试让兔子移动到平面上的鼠标单击位置。

所以我为兔子添加了一个刚体、一个导航网格代理和一个简单的“寻路”脚本。

平面网格渲染器设置为“Navigation Static”、“Generate OffMeshLinks”和“Walkable”

现在,一旦兔子接近给定的目的地,它就不会停下来,而是在目的地周围“跑来跑去”。

这是我的脚本

using UnityEngine;
using UnityEngine.AI;

public class Pathfinding : MonoBehaviour {

private NavMeshAgent agent;
private Animator animator;

// Use this for initialization
void Start() {
    agent = GetComponent<NavMeshAgent>();
    animator = GetComponent<Animator>();
}

// Update is called once per frame
void Update() {
    RaycastHit hit;

    if(Input.GetMouseButtonDown(0)) {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if(Physics.Raycast(ray, out hit)) {
            agent.SetDestination(hit.point);
            animator.SetInteger("AnimIndex", 1);
            animator.SetBool("Next", true);
        }
    }
}
}

还有一张我的兔子物品的照片;)

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    停车距离应 > 0

    【讨论】:

    • 我试过了(值 0,0.5,1,2,5,10),它只改变了圆的半径它看起来像兔子向目的地跑,但它确实不要停在停止距离,我创建了一个简短的 youtube 视频来显示问题(停止距离为 10)youtube.com/watch?v=vbdYTZgJce8&feature=youtu.be
    【解决方案2】:

    其实我错了,更新 Unity 并没有解决问题,但我认识到问题在于动画“根变换位置 (XZ)”/烘烤成姿势被停用。

    我也把我的脚本改成了

    using UnityEngine;
    using UnityEngine.AI;
    
    public class Pathfinding : MonoBehaviour {
    
    private NavMeshAgent agent;
    private Animator animator;
    
    private bool run = false;
    
    // Use this for initialization
    void Start() {
        agent = GetComponent<NavMeshAgent>();
        animator = GetComponent<Animator>();
    }
    
    // Update is called once per frame
    void Update() {
        RaycastHit hit;
    
        if(Input.GetMouseButtonDown(0)) {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if(Physics.Raycast(ray, out hit)) {
                Debug.Log("start");
                agent.SetDestination(hit.point);
                animator.SetInteger("AnimIndex", 1);
                animator.SetBool("Next", true);
                run = true;
            }
        }else if(agent.remainingDistance <= agent.stoppingDistance && run) {
            Debug.Log("stop");
            animator.SetInteger("AnimIndex", 0);
            animator.SetBool("Next", true);
            run = false;
        }
    
    
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      相关资源
      最近更新 更多