I’m developping a 2D pixel based space shooter for mobile platform and i’m struggling with a problem. I’m trying to create a rocket with a area of destruction. I’m trying to substract an amount of life to each gameObjects in a specific circle. Everything’s fine until i try to apply the damages. Every enemy has a “healthscript” and a “shotdetection” script. I try to do it through a foreach, but the life always decrease on the “healthscript” of the aimed gameobject. Here are my code (i found it through different only tutorial for some part because it’s my first project):
HealthScript :
using UnityEngine;
/// <summary>
/// Gestion des points de vie et des dégâts
/// </summary>
public class HealthScript : MonoBehaviour
{
/// <summary>
/// Points de vies
/// </summary>
public int hp = 1;
private int damage;
/// <summary>
/// Ennemi ou joueur ?
/// </summary>
public bool isEnemy = true;
public void ApplyDamage() {
damage = gameObject.GetComponent<ShotDetection> ().damage;
hp -= damage;
// Destruction du projectile
// On détruit toujours le gameObject associé
// sinon c'est le script qui serait détruit avec ""this""
if (hp <= 0)
{
SpecialEffectsHelper.Instance.Explosion(transform.position); // Destruction !
if (gameObject.name == "Player") {
gameObject.SetActive (false);
}
else {
Destroy(gameObject);
}
}
}
}
ShotDetection :
using UnityEngine;
using System.Collections;
public class ShotDetection : MonoBehaviour {
//variables du type de shot
private GameObject Shot;
private GameObject DualShot;
private GameObject Rocket;
private GameObject DualRocket;
//variable explosion rocket
private float radius = 10;
private Collider2D[] objectsInRange;
/// <summary>
/// Ennemi ou joueur ?
/// </summary>
public int damage;
void Start() {
GameObject go = GameObject.Find ("Player");
Shot = go.GetComponent<PlayerScript> ().OneShot;
DualShot = go.GetComponent<PlayerScript> ().DualShot;
Rocket = go.GetComponent<PlayerScript> ().OneRocket;
DualRocket = go.GetComponent<PlayerScript> ().DualRocket;
}
void OnTriggerEnter2D(Collider2D collider)
{
// Est-ce un tir ?
ShotScript shot = collider.gameObject.GetComponent<ShotScript> ();
bool isEnemy = gameObject.GetComponent<HealthScript> ().isEnemy;
if (shot != null) {
if (Shot.activeSelf || DualShot.activeSelf) {
// Tir allié
if (shot.isEnemyShot != isEnemy) {
GetComponent<HealthScript>().ApplyDamage();
}
} else if (Rocket.activeSelf || DualRocket.activeSelf) {
if (shot.isEnemyShot != isEnemy) {
Vector2 location = collider.transform.position;
objectsInRange = Physics2D.OverlapCircleAll (location, radius);
foreach (Collider2D col in objectsInRange) {
EnemyScript enemy = col.GetComponent<EnemyScript> ();
if (enemy != null) { // linear falloff of effect float proximity = (location - enemy.transform.position).magnitude; float effect = 1 - (proximity / radius); enemy.ApplyDamage(damage * effect);
if (shot.gameObject != null) {
Destroy(shot.gameObject);
}
damage = shot.damage;
GetComponent<HealthScript>().ApplyDamage();
}
}
}
}
}
}
}
Sorry for bad english, thank in advance