本文主要是介绍Unity3D物体自动躲避障碍物,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Unity版本
2017.4.4f1
基本思路
物体向前发射一个射线,检测到碰撞后,根据碰撞信息选择新的方向。最终结果如下。
具体实现步骤代码
1.物体添加胶囊体碰撞组件CapsuleCollider
通过发射虚拟胶囊体来检测碰撞api
bool Physics.CapsuleCast (Vector3 point1, Vector3 point2, float radius, Vector3 direction, out RaycastHit hit, float maxDistance)
points参数如图所示,radius是Capsule的radius,direction为当前面向,hit为碰撞信息,maxDistance为检测距离:
根据参数申请一下变量并初始化:
//检测距离float distance = 1;//物体速度public float speed = 1;//物体下一帧面向Vector3 nexForward;//capsule端点Vector3 point1, point2;//capsule组件public CapsuleCollider capsule;// Use this for initializationvoid Start(){capsule = GetComponent<CapsuleCollider>();nexForward = transform.forward;point1 = capsule.center - new Vector3(0, 0.5f * capsule.height, 0);point2 = capsule.center + new Vector3(0, 0.5f * capsule.height, 0);}
2.申请CheckCollision函数来检测碰撞并计算下一帧的面向。
检测到碰撞点后,由碰撞点的Normal方向和物体当前面向来计算新面向。主要方法为
nexForward = Vector3.Cross(hit.normal, Vector3.Cross(transform.forward, hit.normal));
这个公式的大概意思为,计算出当前面向向量transform.forward在碰撞面上的投影向量。可以理解为:hit.normal是阳光方向,transform.forward是一个带方向的射线,得到的是射线在面上的投影射线。投影向量作为碰撞后的新方向。
(1)需要排除掉transform.forward//hit.normal的特殊情况;
(2)检测到碰撞点后,需要从碰撞点出发,用新的面向进行二次检测,防止物体卡入角落。
具体代码如下
void CheckCollision(){//动态检测距离,速度越大越需要提前检测distance = speed * Time.deltaTime;RaycastHit hit;//用于二次检测Vector3 hitPoint;//capsulecastif (Physics.CapsuleCast(transform.position + point1, transform.position + point2, capsule.radius, transform.forward, out hit, distance)){hitPoint = hit.point + capsule.radius * hit.normal;//如果朝向垂直于碰撞平面if (Vector3.Cross(transform.forward, hit.normal) == Vector3.zero){nexForward = Vector3.Cross(hit.normal, Vector3.up);}else{nexForward = Vector3.Cross(hit.normal, Vector3.Cross(transform.forward, hit.normal));}bool left = false;bool right = false;//从碰撞点起,向两边检测两边碰撞if (Physics.CapsuleCast(hitPoint + capsule.center - new Vector3(0, 0.5f * capsule.height, 0), hitPoint + capsule.center + new Vector3(0, 0.5f * capsule.height, 0), capsule.radius, nexForward, out hit, distance)){left = true;}if (Physics.CapsuleCast(hitPoint + capsule.center - new Vector3(0, 0.5f * capsule.height, 0), hitPoint + capsule.center + new Vector3(0, 0.5f * capsule.height, 0), capsule.radius, -nexForward, out hit, distance)){right = true;}if (left){if (!right){nexForward = -nexForward;}}//归一化nexForward.Normalize();}}
3.在update函数中更新物体面向,并向前移动
// Update is called once per framevoid Update(){CheckCollision();if (nexForward != Vector3.zero)transform.forward = nexForward;transform.Translate(new Vector3(0, 0, speed * Time.deltaTime));}
这篇关于Unity3D物体自动躲避障碍物的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!