Unity 武器攻击系统

武器攻击系统

这个Demo是一个武器的管理系统,其中对两种武器进行了管理,达到的效果是当手中拿着不同武器的时候会达到不同的攻击效果,包含近程和远程的攻击效果。
共包含六个脚本。

1.角色中挂载的脚本

简单的点击鼠标使角色移动的脚本。

private NavMeshAgent PlayerAgent;
void Start()
{
    PlayerAgent = GetComponent<NavMeshAgent>();//获取寻路组件
   
}


void Update()
{
    if (Input.GetMouseButtonDown(0) )//点击鼠标左键并且没有点击到IU组件
    {
        Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);//创建一条射线
        RaycastHit hit;
        bool isCollide = Physics.Raycast(ray, out hit);
        if (isCollide)
        {
            if (hit.collider.tag == "Ground")//假如点击物体的标签为Ground
            {
                PlayerAgent.stoppingDistance = 0;//停止距离为0
                PlayerAgent.SetDestination(hit.point);//移动到点击位置
            }
        
        }
    }
}

2.武器属性的管理类

写的比较简单,主要是看一下结构

public int attackValue;//武器攻击力
public virtual void Attack() //用于调用重写
{ 

}

3.角色武器识别装备与释放

 public Weapon weapon;//声明一个武器


 
 void Update()
 {
     if (weapon != null&&Input.GetKeyDown(KeyCode.Space))//判断手中是否有武器
     {
         weapon.Attack();
     }
 }
 public void LoadWwapon(Weapon weapon) //装备武器
 {
 this.weapon = weapon;
 }
 public void UnLoadWwapon(Weapon weapon)//卸载武器
 {
     weapon = null;
 }

4.两个武器的处理

1.近战攻击类的武器

 public const string ANIM_ATTSCK = "Attack";
 private Animator anim;

 private void Start()
 {
     anim = GetComponent<Animator>();
 }
 public override void Attack()//重写Weapon中的Attack方法
 {
     GetComponent<Animator>().SetTrigger(ANIM_ATTSCK);//攻击动画的控制
 }

 private void OnTriggerEnter(Collider other)//武器的碰撞管理
 {
     if (other.tag=="Enemy")
     {
         print("棍子打到了"+other.name);
     }
 }

2.法术类的武器

  public float bulletSpeed;
  public GameObject bulletPrefab;
  private GameObject bulletGo;
  private void Start()
  {
      SpawnBullet();
  }
  public override void Attack()
  {
      if (bulletGo != null)
      {
          bulletGo.GetComponent<Rigidbody>().velocity = transform.forward * bulletSpeed;//让魔法球飞出去
          bulletGo.transform.parent = null;// 解除父子关系
          //transform.DetachChildren();//解除父子关系
          bulletGo = null;
          Invoke("SpawnBullet", 0.5f);//0.5秒后生成一个新的魔法球(Invoke()方法大概意思是0.5秒后调用一次"SpawnBullet")
      }
      else 
      {
          return;
      }
      
  }
private void SpawnBullet()
  {
      bulletGo = GameObject.Instantiate(bulletPrefab,transform.position,transform.rotation);//产生一个新的魔法球
      bulletGo.transform.parent = transform;//将新产生的魔法球置为子物体

  }

法术施放稍微麻烦一些,比如火球,需要将火球扔出去,所以火球的预制体上还要挂载一个处理碰撞销毁的脚本。

 private Rigidbody rgd;
 private Collider col;
 void Start()
 {
     rgd = GetComponent<Rigidbody>();//获取刚体
     col = GetComponent<Collider>();//获取碰撞体
 }

 private void OnCollisionEnter(Collision collision)//碰撞检测
 {
     if (collision.collider.tag=="Player") { return; }//如果碰撞标签是角色自己就跳出
     rgd.velocity=Vector3.zero;//保持当前碰撞位置
     rgd.isKinematic = true;//动态重力控制
     col.enabled = false;//将自身隐藏
     Destroy(this.gameObject,1f);//1秒后销毁
 }

Demo中包含脚本挂载的位置和Unity相关参数的设置,有兴趣可以看看。

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值