Question by
Christopher_Mtn · Feb 20, 2016 at 11:36 PM ·
c#triggerontriggerexitontriggerenter2d
OnTriggerEnter/Exit Rapid Firing?
Hello! So, in my game(2D) so far I have made it work so that when I jump onto the SpaceShip the player will be parented to it with this simple command:
using UnityEngine;
using System.Collections;
public class ShipAtmo : MonoBehaviour
{
public GameObject Ship;
public GameObject player;
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
print("this should print if I got the part collision right! ");
player.transform.parent = Ship.transform;
}
}
}
However, when I add this in:
using UnityEngine;
using System.Collections;
public class ShipAtmo : MonoBehaviour
{
public GameObject Ship;
public GameObject player;
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
print("this should print if I got the part collision right! ");
player.transform.parent = Ship.transform;
}
}
void OnTriggerExit2D(Collider2D other)
{
if(other.tag == "Player")
{
player.transform.parent = null;
}
}
}
the scripts begin to rapid fire, so that the character is parented and then unparented repeatedly while he is on the ship. When he gets off of the ship, it does one ontriggerexit call and he is no longer parented. However, he does remain inside the collider2d so why does it freak out while he's there?
Any help is really appreciated, thanks!!!
Comment