- Home /
OnTriggerExit with 2D Colliders?
I want to to make a 2D space game (average) and when I shoot, my sprite keeps going continuously up. I have set a cube surrounding the outside of the game while the "Is Trigger" is selected with this script:
using UnityEngine; using System.Collections;
public class DestroyCollider : MonoBehaviour { void OnTriggerExit(Collider other) { Destroy(other.gameObject); } }
Shots are still escaping and is spamming my hierarchy!? (Sorry im new tho Unity answers)
I was very close the rejecting your question because your code is not formatted.
Those are the weirdest tags ever. That is not good use of them.
I have done that before OnTriggerExit2D and it wouldnt work, but i realised that I had 'Is Trigger' ticked and changed OnTriggerExit2D to OnCollisionExit/Enter2D and really did what I wanted!
Thank you so much for replying too me and giving amazing imformation to proceed onto my game project!
Answer by Chris333 · Jan 18, 2015 at 04:13 PM
Hi,
instead of using
void OnTriggerExit(Collider other)
use the 2D version, because you are using a sprite.
void OnTriggerExit2D(Collider2D other)
http://docs.unity3d.com/500/Documentation/ScriptReference/Collider2D.OnTriggerExit2D.html
You could also change the 3D cube for the level boundary to a empty gameobject and give this object a BoxCollider2D, because you are using 2D sprites.
A good idea is also to take a look into the following project(if you dont know it already) because it is a example of a 2D space shooter with 3D Objects. http://unity3d.com/learn/tutorials/projects/space-shooter
Your answer
Follow this Question
Related Questions
Fps controller script error 0 Answers
Script error 1 Answer
Something is wrong with unity 0 Answers
PlayerController C# Script error? Help! 1 Answer
Changing Force of object 1 Answer