Unity用射线给玩家做一个点哪到哪的功能
写代码之前,要先将地面设置静态,然后添加Navigation,进行烘焙场景,然后在玩家身上挂上NavMeshAgent,就可以用代码控制玩家了Animator an;NavMeshAgent age;public Transform cube;// Start is called before the first frame updatevoid Start(){an = GetComponent
·
写代码之前,要先将地面设置静态,然后添加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);
}
}
更多推荐
所有评论(0)