Using Unity version 4.2.1f
My latest project is a 3D puzzle game which makes use of many moving components. When activated they would rotate or move from point to point. Because of this I needed a way for the player to stay “attached” to them to prevent the platforms from sliding out from under them since some of them move very quickly.
Originally I intended to use OnTriggerEnter and OnTriggerExit events to mess with parenting of the game object. I quickly discovered that when the object is parented OnTriggerExit is no longer fired, a change that the ask section tells me was changed in 3.2. I don’t particularly understand how that works but working around it was fairly easy. The only problem is that it feels a little cheap (for lack of a better word). I’ll of course implement tag checks to prevent it parenting any other objects that pass through it.
Is there any more elegant solution, or one that would be considered “better” for handling this?
void OnTriggerStay(Collider col)
{
if(col.CompareTag("Player"))
{
if(TP_Controller.CharacterController.isGrounded)
{
//Parent object rather than the empty object containing the trigger.
col.transform.parent = this.transform.parent;
}
else
{
col.transform.parent = null;
}
}
}