- Home /
How to make a rigid body fall when space is pressed and freeze on trigger enter?
I am kind of new to coding so this may be pretty simple but I've tried a lot of different things to try and get it to work.
I have 2 objects attached with a rope. They are rigid bodies and I want them to both be frozen, (or kninematic) on the start of the game. As soon as I hit the spacebar I want them to drop and when one of the objects enters an area (a red cube I made as a non colliding trigger) to freeze and the other object to keep moving. any ideas? Thanks.
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-is$$anonymous$$inematic.html
logically it sounds like you want to do-
if getkeydown spacebar, iskinematic = false
on trigger enter, iskinematic = true
what kind of rope?
if u want to drop ur 2 things, use a blue cube. at least its a drop cubes game notakeful...
Answer by aldonaletto · Dec 07, 2012 at 02:46 AM
You could use something like this attached to the trigger object:
// drag the rigidbodies here
var rb1: Rigidbody;
var rb2: Rigidbody;
function Start(){
// freeze the rigidbodies:
rb1.isKinematic = true;
rb2.isKinematic = true;
}
function Update(){
// unfreeze the rigidbodies when space pressed:
if (Input.GetKeyDown("space")){
rb1.isKinematic = true;
rb2.isKinematic = true;
}
}
function OnTriggerEnter(other: Collider){
var rb = other.rigidbody; // who entered the trigger?
if (rb == rb1 || rb == rb2){ // if it's one of our rigidbodies...
if (!rb1.isKinematic && !rb2.isKinematic){ // and both are active...
rb.isKinematic = true; // freeze the rigidbody that entered first
}
}
}
This code will release the rigidbodies to fall when space is pressed, and when one of them touches the trigger, it gets frozen - but the other keeps falling, even if it passes through the trigger volume.
NOTE: Make sure that Use Gravity is checked in both rigidbodies!
Your answer
Follow this Question
Related Questions
rigid body problem 1 Answer
Freezing Rigidbody Rotation 1 Answer
Enemy AI and Rigid Bodies 0 Answers
Colliding with static model 1 Answer
Rigid body enemy 1 Answer