- Home /
How to add rigidbody with script?
I have a cube in the game that when the player collides with it, it should b added a rigidbody to fall down. This is the script that the player has:
function OnCollisionEnter (other : Collision) {
if(other.gameObject.tag == "WoodTag")
{
var rb : Rigidbody = other.gameObject.AddComponent(Rigidbody);
rb.mass = .5;
rb.drag = 2.0;
}
}
the problem is that nothing happens! why?
Do you get inside the function? Put this above var rb : Rigidbody:
Debug.Log ("We got inside");
Does it return the message?
Ok that is the problem then, we are not getting inside the function. Are your tags placed correctly and is the GameObject defined?
Ok then it sounds like your tags. $$anonymous$$ake sure the player is tagged the same.
Change the tag in the script to player, your script isn't accounting for multiple tags. I am writting from my phone sorry for slow replies.
Sorry I had to go urgently, does the error persist? If so, the problem is the tag, you are only accounting for the "WoodTag" not the "Player" tag. Change "WoodTag" to "Player" and see what happens.
Answer by sdgd · Mar 01, 2013 at 09:54 PM
lol I was bugging my head why does not work for me too
OFC it does not work
IT must have a rigidbody to add it
untill you won't have rigidbody inside your script wont activate it self
strange
box 1 (script + rigidbody)
box 2 (nothing)
moving box 1 in to box 2 returnes me debug only 1 that I'm in function not more than 1 time no matter how much I try
moving box 2 in to box 1 does not return me debug
if I give rigidbody to both I get debug both times but only for 1 colision don't know why but still answer is add rigidbody
Above is for OnColision
Bottom is for OnTriggerEnter
the Cube has to have this script AND RigidBody
void OnTriggerEnter (Collider Other){
Debug.Log("In Collider Works");
if (Other.tag == "Player"){
gameObject.rigidbody.useGravity = true ;
gameObject.collider.isTrigger = false ;
gameObject.rigidbody.AddForce(new Vector3(0,-1,0));
}
}
both objects have to have rigidbody if you want that
OnCollisionEnter
works
But if I add a rigidbody to the player, it doesn't behave right wit the character controller..
Q right now is if it works if you give rigidbody to player
after that you need to tweek players rigidbody like:
useGravity = false ;
is kinematic = true ; - not sure about it
if you give rigidbody to player and on colision does not work you need to find something else than on colision maybe a ray or something
I personally don't use predesigned charcontroller but my own design
the Cube has to have this script AND RigidBody
void OnTriggerEnter (Collider Other){
Debug.Log("In Collider Works");
if (Other.tag == "Player"){
gameObject.rigidbody.useGravity = true ;
gameObject.collider.isTrigger = false ;
gameObject.rigidbody.AddForce(new Vector3(0,-1,0));
}
}
is there any thing else you need
once the player touches this collider the object starts using gravity
collider becomes active and he can stand on falling object