ontriggerexit not working
I have code where when the player leaves a platform, the platform should fall. But instead, the player just falls through the platform instantly. The code is:
void Start () {
rb = GetComponent<Rigidbody> ();
}
void OnTriggerExit(Collider other){
if (other.gameObject.CompareTag ("player")){
rb.useGravity = true;
}
}
I have the player set as a trigger and the code is a component of the floor.
Any fixes?
Answer by PizzaPie · Jan 30, 2017 at 05:16 PM
The player shouldn't be a trigger unless you want it to ignore all colliders and pass through them. Also the platform shouldn't be a trigger too cause it wouldn't be possible to walk on it while using physics. So a viable solution would be to create an empty box collider on top of the platform link it to the platform and make it the trigger. And assign that script to the new Trigger Collider
public Rigidbody platformRB;
void OnTriggerExit(Collider col)
{
if(col.gameObject.tag == "Player")
{
if (platformRB != null)
platformRB.useGravity = true;
}
}
OR
change on your code the line :
void OnTriggerExit(Collider col)
to:
void OnCollisionExit(Collision col)
And remove any triggers
Thanks for the reply! OnCollisionExit doesn't seem to work, even without all the triggers, so how do I link the new collider to the platform?
Attach the above script to the new Trigger Collider on the Inspector and Drag and Drop the Platform GameObject to the field platformRB. It should work. Now on the OnCollisionExit, it should work if it is attached on the Platform GameObject. Gonna test it later i ll update you if it works.
Your answer
Follow this Question
Related Questions
ontriggerexit not working 0 Answers
Trigger FinishLine 1 Answer
Problem dealing with colliders in a complex project [SOLVED] 0 Answers
OnTriggerEnter not firing 2 Answers