Обработка коллизий двух разных объектов одним скриптом
Есть два объекта с одинаковым кусочком кода, который я хотела бы загнать в один отдельный скрипт. Мне необходимо сделать, чтоб при столкновении двух объектов убывала переменная health нужного объекта, но у меня не получается
скрипт №1
public class AsteroidBig : MonoBehaviour {
public Transform asteroidMed;
public float speed = 1.5f;
public int health = 10;
void OnCollisionEnter2D(Collision2D theCollision){
if(theCollision.gameObject.name.Contains("laser")){
Laser laser = theCollision.gameObject.GetComponent("Laser") as Laser;
health -= laser.damage;
Destroy (theCollision.gameObject);
}
}
void Update () {
transform.Translate(Vector3.up * Time.deltaTime * speed);
if (health <= 0){
for (int i = 0; i < 3; i++) {
Instantiate (asteroidMed, this.transform.transform.position, Quaternion.Euler (0, 0, Random.Range (0f, 360f)));
Instantiate (asteroidMed, this.transform.transform.position, Quaternion.Euler (0, 0, Random.Range (0f, 360f)));
Instantiate (asteroidMed, this.transform.transform.position, Quaternion.Euler (0, 0, Random.Range (0f, 360f)));
}
Destroy (this.gameObject);
}
}
}
скрипт №2
public class AsteroidMedium : MonoBehaviour {
public float speed = 1f;
public int health = 4;
public float TimeImmortal = 3f;
void OnCollisionEnter2D(Collision2D theCollision){
if(theCollision.gameObject.name.Contains("laser")){
Laser laser = theCollision.gameObject.GetComponent("Laser") as Laser;
health -= laser.damage;
Destroy (theCollision.gameObject);
}
}
void Update () {
transform.Translate(Vector3.up * Time.deltaTime * speed);
if (health <= 0){
Destroy (this.gameObject);
}
}
}
Кусочек кода, который я пыталась загнать в отдельный скрипт
public class Stalk : MonoBehaviour {
void OnCollisionEnter2D(Collision2D theCollision){
if(theCollision.gameObject.name.Contains("laser")){
Laser laser = theCollision.gameObject.GetComponent("Laser") as Laser;
health -= laser.damage;
Destroy (theCollision.gameObject);
}
}
}
Скрипт лазера
public class Laser : MonoBehaviour {
public float lifetime = 2.0f;
public float speed = 5.0f;
public int damage = 1;
void Start () {
Destroy (gameObject, lifetime);
}
void Update () {
transform.Translate(Vector3.up * Time.deltaTime * speed);
}
}
Ответы (1 шт):
Автор решения: Yaroslav
→ Ссылка
Скрипт №0
public enum kAsteroidType { Big, Medium }
public class Asteroid : MonoBehaviour {
public kAsteroidType aType; // <-----
public Transform asteroidMed;
public float speed;
public int health;
public float TimeImmortal;
void OnCollisionEnter2D (Collision2D theCollision) {
if (theCollision.gameObject.name.Contains("laser")) {
Laser laser = theCollision.gameObject.GetComponent("Laser") as Laser;
health -= laser.damage;
Destroy(theCollision.gameObject);
}
}
void Update () {
transform.Translate(Vector3.up*Time.deltaTime*speed);
if (health <= 0) {
if (aType == kAsteroidType.Big) {
for (int i = 0; i < 3; i++) {
Instantiate(asteroidMed, this.transform.transform.position, Quaternion.Euler(0, 0, Random.Range(0f, 360f)));
Instantiate(asteroidMed, this.transform.transform.position, Quaternion.Euler(0, 0, Random.Range(0f, 360f)));
Instantiate(asteroidMed, this.transform.transform.position, Quaternion.Euler(0, 0, Random.Range(0f, 360f)));
}
}
Destroy(this.gameObject);
}
}
}
Если атрибуты не указываются в префабе, то в коде так
void Start () {
switch (aType) {
case kAsteroidType.Big: {
speed = 1.5f;
health = 10f;
} break;
case kAsteroidType.Medium: {
speed = 1f;
health = 4f;
TimeImmortal = 3f;
} break;
}
}