- Home /
TriggerEnter, useGravity, Cubes are not falling down
Hello, i have problem.
I got two cubes. Cube > with a collider and rigidbody, useGravity is unchecked. Cube > with a collider and script attached on it. Script:
var ObjectToFall : UnityEngine.GameObject;
function OnTriggerEnter ( other : Collider ) { ObjectToFall.rigidbody.useGravity = true; }
So when i collide it with my playercontroller (2d platform tutorial), first cube will fall down, checking on the useGravity. It not works, why? (If i push the first cube, it slowy moves in direction where i have pushed it)
Answer by skovacs1 · Nov 22, 2010 at 05:33 PM
Your description gets very confusing towards the end when you suddenly start talking about a character controller and pushing. What do you mean by this?
There are several reasons why OnTriggerEnter may not work:
- Your GameObject with the trigger script does not have a Rollider.
- Your GameObject with the trigger script's Collider does not have the isTrigger checked.
- Your GameObject with the trigger script's trigger script instance's ObjectToFall instance is not set.
- Your GameObject with the Rigidbody does not have a Rollider.
- Your GameObject with the Rigidbody's Rigidbody is asleep.
to name a few.
Since 1-4 are pretty rudimentary, your most likely culprit above is 5. Unless your rigidbody is moving, changing, etc. it will be asleep and will not set off OnTriggerEnter. See this doc for more. The simple solution is to force it to wake up within some proximity to your trigger, but this does not scale well as for n rigidbodies this becomes much more costly and complicated to handle. For those cases, you may want to use a different mechanism than depending upon OnTriggerEnter.
To force awake, you could add something in your trigger script like:
var range : float = 3.0f;
function Update() { if(ObjectToFall && (ObjectToFall.transform.position - transform.position).magnitude <= range) ObjectToFall.WakeUp(); }
Answer by Bampf · Nov 22, 2010 at 06:54 PM
It sounds like you want the player to enter an area represented by cube A, causing cube B to fall. Is that right?
You are putting code in OnTriggerEnter. Make sure Is Trigger on cube A is true (checkbox is checked.)
Make sure ObjectToFall on cube A was assigned the cube that you want to fall (cube B).
If neither of these help, it might help to narrow down the problem. While the game is running, select cube B in the Inspector. You should be able to see the Use Gravity checkbox switch on when the player hits cube A. You can also try toggling Use Gravity on manually. This will help you narrow down whether the problem is with the cube A trigger, or the gravity on cube B.
Answer by pocikanec · Nov 23, 2010 at 01:53 PM
Thanks for fast replys, iam really an idiot! isTrigger was unchecked... Now my next question, how to make isTrigger checked and make the cube like it is unchecked. So, the isTrigger = true & my character dont go trough it
Your answer
Follow this Question
Related Questions
Trigger Falling Object 1 Answer
Walking on walls JS 2 Answers
how to set rotation / position of an object on trigger? 2 Answers
Distinguish between two colliding objects 1 Answer
character gravity problem 1 Answer