设置一个胶囊提代替人物进行测试

添加组件 Nav Mesh Agent

添加代码控制

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;  //现在导航在unity.AI中
public class textNav : MonoBehaviour
{
    private NavMeshAgent agent;
    private Ray ray;
    void Start()
    {
        agent = transform.GetComponent<NavMeshAgent>();
    }

    // Update is called once per frame
    void Update()
    {
        
        if (Input.GetMouseButtonDown(0))
        {
            //从摄像机发出到点击坐标的射线

            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                //显示射线,只有在scene视图中才能看到
                Debug.DrawLine(ray.origin, hit.point);
                Debug.Log(hit.point);//碰撞坐标
                agent.SetDestination(hit.point);
            }
        }
      
    }
}

实现在game视图中点击,人物导航至点击位置。

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐