How to kill enemy when the bullet hits the BoxColllider2D?
Hello! I´m currently working in a game and I´m having trouble here. My enemy has two colliders (BoxCollider2D and CircleCollider2D). I use the CircleCollider2D to trigger the enemy (if the player is inside of the CircleCollider2D, the enemy will attack him). I knew that if my player shoots, bullet will collide with the CircleCollider2D first. I´m having trouble cause I only want to apply damage when the bullet hits the BoxCollider2D. Can you help me solve this?
Here´s my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Bullet : MonoBehaviour {
public float vel = 5f;
Rigidbody2D rb;
BoxCollider2D enemyhit;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
print ("oribullet:" + transform.rotation.z);
if (transform.rotation.z !=-1){
rb.AddForce (Vector2.right * vel);
}else{
rb.AddForce (-Vector2.right * vel);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "bomber")
{
DestroyObject (other.gameObject);
Destroy (gameObject);
}
}
}
Answer by Harinezumi · Feb 08, 2018 at 04:14 PM
Your CircleCollider2D should be a trigger, not a physically blocking collider, and your BoxCollider2D should be a non-trigger collider. Then you can detect if the player entered the range in OnTriggerEnter(Collider), and detect if it was shot in OnCollisionEnter(Collision).