How do I make a platform fall after a player has left it?
I'm trying to create a platform that will stay in place while the player is on it, but directly after they leave, it falls down. I'm using this code:
public class platformFall : MonoBehaviour {
public Rigidbody rb;
void Start(){
rb.isKinematic = true;
}
void OnTriggerExit(Collider other) {
rb.isKinematic = false;
Debug.Log ("Exit");
}
void OnTriggerEnter(Collider other){
Debug.Log ("Enter");
}
}
on the platform. The player and the platform both have colliders, and I can see that they are set up properly (The collider for the platform is far above it). However, when the player moves about halfway across the platform, it falls early. They should definitely still be in the trigger area, but it still falls each time. Even stranger, the Debug.Log tells me that the player "enters" the trigger area many times, even though this should only happen once. Is there any way to fix this? I've tried a few other ways to do this but always encounter problems.
Answer by Jason2014 · Feb 24, 2016 at 09:19 AM
Add Rigidbody to the platform with "Use Gravity" property switched off and replace "OnTriggerExit" to "OnCollisionExit". Triggers works when player can "enter" in a specified radius of collider set as trigger. In "OnCollisionExit" function just switch "Use Gravity" to on.
Answer by Statement · Feb 24, 2016 at 09:34 AM
However, when the player moves about halfway across the platform, it falls early.
It sounds like you should make your trigger bigger.
They should definitely still be in the trigger area, but it still falls each time.
Given your trigger is large enough, it is mysterious. Maybe you have multiple colliders on your player that get turned off, or leave the area?
Even stranger, the Debug.Log tells me that the player "enters" the trigger area many times, even though this should only happen once.
If you have multiple colliders on your player, this is not strange.
Here's something you can try: Checking if the object in the trigger belongs to the player (so it doesn't register the object you'd stand on) and ref counting, in case the player is made of multiple colliders.
// NOTE: Requires collider game objects tagged Player
public class platformFall : MonoBehaviour
{
public Rigidbody rb;
int colliders;
void Start() {
rb.isKinematic = true;
}
void OnTriggerEnter(Collider other) {
if (IsPlayer(other) && colliders++ == 0)
PlayerEntered();
}
void OnTriggerExit(Collider other) {
if (IsPlayer(other) && --colliders == 0)
PlayerExited();
}
bool IsPlayer(Collider other) {
return other.tag == "Player";
}
void PlayerEntered() {
print("Enter");
}
void PlayerExited() {
print("Exit");
rb.isKinematic = false;
}
}
Your answer

Follow this Question
Related Questions
Trigger FinishLine 1 Answer
How i make one trigger work if there only one object in it ? 0 Answers
How to detect collision of two moving characters? 1 Answer
How to handle enemy dying inside trigger collider? 2 Answers
Problem with 2D Triggers 1 Answer