The question is answered, right answer was accepted
Exit from OnTriggerEnter doesn't work.
Hello,
I've a problem, when I enter in the trigger everything works fine but when I exit from this my character continues to be like if he's in it. I've tried with OnTriggerExit and with OnTriggerStay but it's the same. Can you help me please ? :)
Here is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Child : MonoBehaviour
{
[SerializeField]
Transform platform;
void Start ()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Player")
{
other.transform.parent = platform.transform;
}
}
}
In the script you have provided, there is no "OnTriggerExit".
I've made some researches and my code is good now (i think) :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Child : $$anonymous$$onoBehaviour
{
[SerializeField]
GameObject platform;
void Start ()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Player" && other.tag == "Dynamic" )
{
other.transform.parent = platform.transform;
}
}
void OnTriggerExit2D(Collider2D other)
{
if(other.tag == "Player" && other.tag == "Dynamic")
{
other.transform.parent = null;
}
}
}
but i have an other problem. I've changed my character controller code and now nothing happen, even with the OnTriggerEnter. This is veeeeery annoying...
Copy paste your that code too. And make sure you have attached the Trigger colliders on those bodies.
Answer by Prototype-Industrie · Dec 13, 2016 at 05:11 PM
Here is the code i have now :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Child : MonoBehaviour
{
[SerializeField]
GameObject platform;
void Start ()
{
}
void OnTriggerStay2D(Collider2D other)
{
if(other.tag == "Player")
{
other.transform.parent = platform.transform;
}
if(other.tag == "Dynamic")
{
other.transform.parent = platform.transform;
}
}
void OnTriggerExit2D(Collider2D other)
{
if(other.tag == "Player")
{
other.transform.parent = null;
}
if(other.tag == "Dynamic")
{
other.transform.parent = null;
}
}
}
It works well with both "Dynamyc" and "Player objects but sometimes my character still get in the trigger when it should not. i don't know how to correct it. It does not happen often but it is annoying when it happens.
OnTriggerStay2D is called every frame while the object is within a trigger collider area. You may want to consider using OnTriggerEnter2D ins$$anonymous$$d.
This is what I used first but it's worse, it's not working once in two: /
couple of things, may not be the problem, but it seems you never assign the platform transform variable from the code, you may want to just declare it public ins$$anonymous$$d of using the serialisable attribute, unless you really don't want the other classes to access it, and you may want to use "else if" or a "||" comparison, as other.tag will never be both "Player" and "Dynamic".
if(other.tag == "Player" || other.tag == "Dynamic")
{
other.transform.parent = null;
}
Follow this Question
Related Questions
Detect the objects staying on top of the Particles 0 Answers
Setting up a function for sorting specific triggers entered based on a height integer. 0 Answers
How to move an object on a moving platform with the platform 0 Answers
LookAt in 2d is not working Again :/ 0 Answers
Help me with gui please (C#) 2 Answers