Question by
aesselman0830 · Oct 26, 2015 at 11:57 AM ·
c#javascriptcollisionnoobfalling
Make object fall when user enters space
I'm working on a project and am an absolute noob. I simply want an apple to fall from a tree when the player walks by that particular tree. I've been playing around with scripting but can't seem to figure it out. Any help would be very much appreciated. Thanks!
Comment
Best Answer
Answer by Developer1212 · Oct 26, 2015 at 12:20 PM
Ok , first make a separate cube or square under this tree, scale it to how large you want the area to be and make sure the tree is at the center.Try to camouflage the block but don't delete the box collider. Now for this to work , you need to do this:
Add a rigidbody to your apple(turn the gravity OFF).
2.Write this code:
#pragma strict
var Apple : Rigidbody; //drag your apple to this variable when done scripting
function OnCollisionEnter (theCollision : Collision) { //the collision function.
if(theCollision.gameObject.name == "Whatever you called that cube"){ /*if the collision variable we set above in
the parenthesis is called whatever you named the cube we made earlier on;*/
Apple.rigidbody.useGravity = true; //enables gravity for the apple, making it drop.
}
}
This should work. Hope it helps!