Unity3D之战斗系统的攻击范围判定

前言

对于RPG游戏而言,战斗系统中的角色攻击范围判定是必不可少的,下面就讲其中一种判定方式

实现原理

为了在怪物能在这个扇形区域的范围内能被角色近战攻击,就在计算出

  • 角色与怪物的距离
  • 角色与怪物的夹角

参考代码

private void SelectMonster()
  {
    
    
    // 获取所有怪物
    GameObject[] monster = GameObject.FindGameObjectsWithTag("Monster");
    List<GameObject> monsterList = new List<GameObject>();
    // 把在攻击范围里面的怪物放入列表
    foreach (var item in monster)
    {
    
    
      float distance = Vector3.Distance(transform.position, item.transform.position);
      float angle = Vector3.Angle(transform.forward, item.transform.position - transform.position);
      if (distance < 50f && angle < 50f)
      {
    
    
        monsterList.Add(item);
      }
    }

    foreach (var item in monsterList)
    {
    
    
      // 其他效果

      // 血量移除
    }
  }

效果预览

后话

如果需要其他判定,例如矩形判定或其他,可以参考这篇:https://blog.csdn.net/songhuanfei2017/article/details/90917948

Enjoy ~

猜你喜欢

转载自blog.csdn.net/a924282761/article/details/131037438
今日推荐