unity的学习----NavMeshAgent,自动寻路

让摄像机实现上楼,模拟人在上楼,用到了这个unity内置的寻路插件NavMesh.通过这个,可以实现确定一个目标点,角色会自动寻路过去。

这是unity官网中的一个图,关于角色可行走的路线。

以该场景为例子。


先选中我可行走的区域,比如楼梯,在右方navvigation处勾选Navigation Static,(Generate  offMeshLinks决定的是两个不相邻的障碍物之间是否可以直接跨越)在Navigation Area处选择walkable,进行bake。

                                                                   

Bake时这些参数要注意,

  • Agent Radius defines how close the agent center can get to a wall or a ledge.
  • Agent Height defines how low the spaces are that the agent can reach.
  • Max Slope defines how steep the ramps are that the agent walk up.
  • Step Height defines how high obstructions are that the agent can step on.

烘焙结束后,可见可行的区域都变成了蓝色。

路径确定了,接下来就是对于角色的控制了,点击角色,给他一个component,,接着给它添加一个移动的脚本就实现了



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class navmove : MonoBehaviour {


	private NavMeshAgent agent;


	void CameraRotateToLookAt(Transform target,float rotateSpeed)
	{
		//取得目标物体相对于相机的法向量
		Vector3 normalize = Vector3.Cross(this.transform.forward,target.transform.position-this.transform.position);
		float angles = Vector3.Angle(this.transform.position,target.transform.position);

		//以该法向量为轴进行旋转
		this.transform.Rotate(normalize,Time.deltaTime*rotateSpeed,Space.Self);
		Debug.Log(angles);
		if(angles == 0)
		{
			rotateSpeed = 0;
		}
	}

	void Start () {
		agent = GetComponent<NavMeshAgent> ();
		
	}

	// 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)) {
			
				Vector3 point = hit.point;
//				transform.LookAt (new Vector3 (point.x, transform.position.y, point.z));
				CameraRotateToLookAt(hit.transform,1.0f);

			}
			agent.SetDestination (hit.point);
		
		}
		
		
	}



	  


 



}


其中

CameraRotateToLookAt(Transform target,float rotateSpeed)

该函数是让摄像机缓慢的转动,不会猛地一转,让眼睛很不舒服。

将脚本添加给角色,就完成了。



!!!容易出现的问题:

1,"SetDestination" can only be called on an active agent that has been placed on a NavMesh。

NavMeshAgent不在NavMesh上,或场景调整后未及时烘焙。

2,在脚本中使用NavMeshAgent时,要注意命名空间using unityengine.AI;




NavMeshAgentUnity引擎中的一个组件,用于控制角色的移动和寻路。通过使用NavMeshAgent,开发者可以为角色创建一个导航网格,标记可到达和不可到达的区域,并使角色能够自动寻找最优路径,避免与其他Agent和障碍物发生碰撞。NavMeshAgent组件自带了寻路和空间推理的脚本,可以帮助角色朝着设定的目标移动,并避免与其他Agent相互干扰。 要为角色添加NavMeshAgent组件,可以通过在角色的脚本中添加以下代码来实现:private NavMeshAgent agent;在Unity中,还有一个Unity NavMesh 2D寻路的功能可用于2D游戏的导航和寻路。你可以通过下载一个相关的存储库或将其添加到项目的Package Manager清单中来使用这个功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Unity3D实用组件:NavMesh Agent](https://blog.csdn.net/float_freedom/article/details/126179067)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [NavMeshPlus:Unity NavMesh 2D寻路](https://download.csdn.net/download/weixin_42129797/15108830)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值