Unity 您所在的位置:网站首页 unity敌人ai制作的几种方法 Unity

Unity

2024-07-09 21:07| 来源: 网络整理| 查看: 265

系列文章目录         一 、 人物移动和转向         二、  人物跳跃和落地         三、  人物攻击和判定         四、  人物受伤和死亡

目录

目录

前言

一、攻击的准备

二. 武器跟随手部实现

三. 攻击动作逻辑实现

四. 武器攻击的判定

武器的脚本:

开始攻击方法:

逐帧进行攻击检测

简单的判定方法:

五. 受攻击实现

总结

前言

本文记录本人在Unity3D中的案例的人物攻击和受伤逻辑的实现,受伤要结合被攻击对象的实现,所以这里主要还是攻击的实现。

  实现效果:

一、攻击的准备

为人物的武器添加对应的组件,确保有控制器脚本,跟随手部脚本和box碰撞检测。 

二. 武器跟随手部实现 [DefaultExecutionOrder(9999)] public class FixedUpdateFollow : MonoBehaviour { public Transform toFollow; private void FixedUpdate() { transform.position = toFollow.position; transform.rotation = toFollow.rotation; } }

比较简单,找到人物手部的骨骼对象,每帧使得武器中心和它的位置和方向一致就行。

三. 攻击动作逻辑实现

这里默认只有落地情况才能攻击,当然还可以实现空中攻击。 

 攻击状态机实现:

Attack1到4对应脚本:

实现传递攻击状态给playerController脚本 ,激活武器和武器特效。

public class MyMeleeEffect : StateMachineBehaviour { public int index; // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { //动作开始时加载对应动作特效 MyPlayerController ctrl = animator.GetComponent(); ctrl.m_playWeapon.timeEffects[index].Activate(); ctrl.isAttacking = true; ctrl.PlayAttackAudio(); //将武器设置为attack状态 ctrl.MyMeleeAttackStart(); } // OnStateExit is called when a transition ends and the state machine finishes evaluating this state override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { MyPlayerController ctrl = animator.GetComponent(); ctrl.isAttacking = false; } } 四. 武器攻击的判定

一开始想着攻击判定用由武器的OntriggerEnter和OntriggerStay触发的。

也不是不行,但是攻击的判定准确率不算高,所以后面还是用比较严谨的球投射判定方法。

基本思路:

武器处于攻击状态时,记录武器上各个攻击点的偏移向量,在攻击向量范围发射球状射线检测碰撞体。如图,灰色的球体是武器上的位置不断变化的攻击点,白色的线段是武器点的运动轨迹(这里设置了3个攻击点);

 

武器的脚本:

MeeleWeapon主要的成员:

public AttackPoint[] m_AttackPoints;//武器身上的攻击点 protected Vector3[] previousPosition;//开始攻击时的位置 protected static RaycastHit[] s_RaycastHits = new RaycastHit[32]; protected static Collider[] s_colliders = new Collider[32]; [Serializable]//使结构体序列化 public struct AttackPoint { public float radius; public Vector3 offset; public Transform Attackroot; } 开始攻击方法:

在武器攻击的动画中添加事件调用。

按照攻击动画中的有效攻击时间,挂上开始和结束的事件。(这里的事件好像是要在animator对象上实现才行)

在人物控制脚本中调用武器的开始/结束攻击方法:

武器中的开始攻击和结束攻击:

开始攻击把开始的攻击点坐标存贮在一个数组中:结束攻击把状态设置为false就行。

public void OnStartAttack(bool throwingAttack) { isAttacking = true; previousPosition = new Vector3[m_AttackPoints.Length]; for(int i = 0; i < m_AttackPoints.Length; i++) { Vector3 worldPos = m_AttackPoints[i].Attackroot.position + m_AttackPoints[i].Attackroot.TransformVector(m_AttackPoints[i].offset); previousPosition[i] = worldPos;//存储每个点的起始攻击坐标 } } public void OnEndAttack() { isAttacking = false; } 逐帧进行攻击检测

攻击检测主要用到Physics中的SphereCastNotAlloc函数进行球体碰撞检测:

官方文档的描述: 

public static int SphereCastNonAlloc(Vector3 origin, float radius, Vector3 direction, RaycastHit[] results, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

沿direction方向投射球体,并储存在results缓冲器中。这个是Physics.SphereCastAll的变种,而是把查询的结果储存在提供的数组中。这个仅计算碰到对象的多少,储存到缓冲器中,并没有特定的顺序。它不能保证它只存储最近的碰撞。不产生垃圾。

所以用SphereCastAll也可以,但是会产生比较多的垃圾。

private void FixedUpdate() { if(isAttacking) { for (int i = 0; i < m_AttackPoints.Length; i++) { Vector3 worldPos = m_AttackPoints[i].Attackroot.position + m_AttackPoints[i].Attackroot.TransformVector(m_AttackPoints[i].offset);//此帧的攻击点坐标 Vector3 attackVector = worldPos - previousPosition[i];//攻击的向量 Ray myRay = new Ray(worldPos,attackVector.normalized);//投射射线 int contectNumber = Physics.SphereCastNonAlloc(myRay,m_AttackPoints[i].radius,s_RaycastHits,attackVector.magnitude,~0,QueryTriggerInteraction.Ignore);//进行球体碰撞检测 for(int j = 0; j < contectNumber; j++)//逐一检测碰撞的目标 { Collider d = s_RaycastHits[j].collider;//需要对方有一个碰撞器 if(d) { CheckDamage(d,m_AttackPoints[i]);//开始计算伤害 } } } } } 简单的判定方法:

然后是简单,但是不是很严谨的trigger检测方法:(这里要注意Trigger双方都要有collider,至少一方要有rigidbogy),而且要注意在unity的Physics设置中是能进行碰撞检测的,不然也触发不了;

public class MyPlayerWeapon : MonoBehaviour { public int damage = 1; //攻击时的武器特效 public TimeEffect[] timeEffects; public LayerMask m_TargetLayer; public bool isAttacking; public Transform attackPoint; //事件可以绑定在状态机上,也可以就绑定在动画animation中 private void OnTriggerEnter(Collider other) { if (!isAttacking)//非攻击状态 { print("非attack状态!"); return; } isAttacking = false;//一次碰撞只产生一次伤害 MyDamageable d = other.GetComponent(); if (d == null) return; CheckDamage(other); } void CheckDamage(Collider col) { MyDamageable.DamageMessage data = new MyDamageable.DamageMessage() ; data.damager = this; data.amount = damage; data.damageSource = attackPoint.position; var d = col.GetComponent(); if(d) d.OnGetDamage(data); } }

 物理检测设置:(打勾的表示双方可以进行检测)

五. 受攻击实现

在受攻击对象添加Damageable脚本,脚本包含本身的血量、受攻击的处理事件(每个对象受攻击后的处理方式不同),用来接收受击信息,受击信息包括受击伤害、受击点、受击方向、攻击对象、攻击对象坐标等。

主要用到一个多播委托,在unity编辑器中赋予受击事件,脚本中的适当时机调用受击后的事件。

public partial class MyDamageable : MonoBehaviour { [SerializeField] protected int curHitPoints; protected Action schedule; public int myMaxHipPoints; public bool invincible = false;//无敌的 public TextMesh m_HitPointsShow; public UnityEvent OnDamage, OnDeath, OnResetDamage, OnBecmeVulnerable, OnHitWhileInvulunerable; public void OnReSpawn() { curHitPoints = myMaxHipPoints; } private void Awake() { curHitPoints = myMaxHipPoints; invincible = false;//开始可以有一段无敌时间 if(m_HitPointsShow) m_HitPointsShow.text = curHitPoints.ToString(); } //受到攻击时调用 public bool OnGetDamage(MyDamageable.DamageMessage data) { if (invincible || curHitPoints


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有