public void attack(bool isenemy)//进行攻击,bool表示是否为敌人,这个函数,将会让武器射出子弹 { if (canattack) { wepCooldown = wepingRate; Transform shotTransform = Instantiate(wepprefab) as Transform; move move = shotTransform.GetComponent <move>(); shotTransform.position = transform.position;//继承武器的位置和旋转 shotTransform.rotation = transform.rotation; if (move != null)//该子弹有move说明是远程攻击,设置其move的方向 { move.right(); } else //该子弹没有move说明是近战攻击 { shotTransform.parent = transform; //将子弹的父物体设置为该武器 } shotscrips shot = shotTransform.GetComponent <shotscrips>();//获取子弹的shot组件 if (shot != null) { shot.isenemy = isenemy; shot.force = force; } } }
private void OnTriggerEnter2D(Collider2D collision) { shotscrips shot = collision.gameObject.GetComponent <shotscrips>(); if (shot != null) { if (shot.isenemy != isenemay) { Damage(shot.damage); shot.destroy(this.gameObject); } } }