/// <summary> /// 射击 /// </summary> /// <param name="isEnemy">是否是敌人的子弹</param> public void Attack(bool isEnemy) { if (CanAttack) { if (isEnemy) { //SoundEffectsHelper.Instance.MakeEnemyShotSound(); } else { //SoundEffectsHelper.Instance.MakePlayerShotSound(); } shootCooldown = shootingRate; // 创建一个子弹 var shotTransform = Instantiate(shotPrefab) as Transform; // 指定子弹位置 shotTransform.position = transform.position; // 设置子弹归属 missileShot shot = shotTransform.gameObject.GetComponent <missileShot>(); shot.isClone = true; // 设置子弹运动方向 ScrollingScript move = shotTransform.gameObject.GetComponent <ScrollingScript>(); if (move != null) { // towards in 2D space is the right of the sprite move.direction = this.transform.right; } } }
void OnTriggerEnter2D(Collider2D otherCollider) { missileShot shot = otherCollider.gameObject.GetComponent <missileShot>(); if (shot != null) { Damage(shot.damage); // 销毁子弹 // 记住,总是针对游戏的对象,否则你只是删除脚本 Destroy(shot.gameObject); } }