Answer the question
In order to leave comments, you need to log in
What to do if Collision2D doesn't work?
here is the script of 1 object
using UnityEngine;
using System.Collections;
public class EnemyScript : MonoBehaviour
{
public int health = 2;
public GameObject boom;
public int damage = 1;
void Start()
{
boom = GameObject.Find("Boom");
}
void OnTriggerEnter2D(Collider2D collider)
{
if (collider.gameObject.tag == "Bullet")
{
GetComponent<AudioSource>().Play();
health = health - 1;
}
if (health <= 0)
{
GameObject.Find("EnemySpawn").GetComponent<GameController>().KilledEnemy();
boom.transform.position = new Vector2(transform.position.x, transform.position.y);
boom.GetComponent<ParticleSystem>().Play();
boom.GetComponent<AudioSource>().Play();
Destroy(gameObject);
}
}
}
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour
{
public float lifetime = 2.0f;
public float speed = 5.0f;
public int damage = 1;
// Use this for initialization
void Start()
{
Destroy(gameObject, lifetime);
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.up * Time.deltaTime * speed);
}
void OnTriggerEnter2D(Collider2D collider)
{
if (collider.gameObject.tag == "Enemy")
{
Destroy(gameObject);
}
}
}
Answer the question
In order to leave comments, you need to log in
For triggers to fire, one of these objects must have a Rigidbody2D component
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question