- Home /
Unity2D onTriggerExit2D not working!! Help!!
Hey guys,
So basically I am creating a 2D platformer with Unity and I have run into a problem.
I am trying to create a boundary box around my map, so that when enemies inside hit the edges of the map, they switch direction.
I am quite sure that the code i have written is good but can someone please check it out for me? It is driving me crazy as it should be working!!
using UnityEngine;
using System.Collections;
public class Boundaries : MonoBehaviour
{
private GameObject Enemies;
// Use this for initialization
void Start ()
{
Enemies = GameObject.FindGameObjectWithTag("Enemy");
}
// Update is called once per frame
void Update ()
{
}
void onTriggerExit2D(Collider2D other)
{
Debug.Log ("Left The Building");
if (other.gameObject.tag == "Enemy")
{
Debug.Log ("Left The Building");
other.gameObject.transform.rotation = Quaternion.Euler (transform.rotation.x, 180, transform.rotation.z);
//Enemies.gameObject.transform.rotation = Quaternion.Euler (transform.rotation.x, 180, transform.rotation.z);
}
}
}
Just to let you know the boxCollider2D around the map has been set to trigger, and the enemies have two box collider2D's, one is a trigger and one isn't.
Not even the debug.log's are working !! :S
Thanks :)
Hey guys, managed to solve it ... thanks for all your answers :)
Answer by m_pangolin · Dec 28, 2014 at 04:52 PM
Instead of writing onTriggerExit2D you should write OnTriggerExit2D (Capital O)
Oh my god, just for a capitalization issue! thanks so much :D haha great now the debug.log's are working ... but i still cant get the sprite to flip round and go the other direction.... any ideas?
Answer by ShootingStar · Dec 28, 2014 at 10:27 PM
Here's one way to get the sprite to flip and go to the other direction. This first flips the gameobject, then inverts its velocity to the opposite direction:
other.gameObject.transform.Rotate(0, 180, 0, Space.Self);
other.gameObject.rigidbody2D.velocity = -other.gameObject.rigidbody2D.velocity;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Flip over an object (smooth transition) 3 Answers
How to do a sprite flip 1 Answer
Problem with OnTriggerExit(). If loop not passing to next loop. 1 Answer