写代码之前,要先将地面设置静态,然后添加Navigation,进行烘焙场景,然后在玩家身上挂上

NavMeshAgent,就可以用代码控制玩家了

 

 

Animator an;
    NavMeshAgent age;
    public Transform cube;
    // Start is called before the first frame update
    void Start()
    {
        an = GetComponent<Animator>();
        age = GetComponent<NavMeshAgent>();
        //age.destination = cube.position;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                age.SetDestination(hit.point);
                transform.LookAt(hit.point);
            }
        }
        
        if (age.remainingDistance<=1f)
        {
            an.SetFloat("run",0);
        }
        else
        {
            an.SetFloat("run",1);
        }
    }

Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐