Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

How to kill enemy when the bullet hits the BoxColllider2D?

$
0
0
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(); } // 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); } } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>