Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by smirlianos · Mar 01, 2013 at 07:15 PM · javascriptcollisionrigidbodyjavaaddcomponent

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?

Comment
Add comment · Show 16
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nercoe · Mar 01, 2013 at 07:17 PM 1
Share

Do you get inside the function? Put this above var rb : Rigidbody:

 Debug.Log ("We got inside");

Does it return the message?

avatar image Nercoe · Mar 01, 2013 at 07:22 PM 1
Share

Ok that is the problem then, we are not getting inside the function. Are your tags placed correctly and is the GameObject defined?

avatar image Nercoe · Mar 01, 2013 at 07:28 PM 1
Share

Ok then it sounds like your tags. $$anonymous$$ake sure the player is tagged the same.

avatar image Nercoe · Mar 01, 2013 at 07:45 PM 1
Share

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.

avatar image Nercoe · Mar 02, 2013 at 09:34 AM 1
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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));
     }
 }
Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image smirlianos · Mar 02, 2013 at 10:07 AM 0
Share

I didn't actually understood what you are saying...!

avatar image sdgd · Mar 02, 2013 at 11:52 AM 0
Share

both objects have to have rigidbody if you want that

OnCollisionEnter

works

avatar image smirlianos · Mar 02, 2013 at 11:55 AM 0
Share

But if I add a rigidbody to the player, it doesn't behave right wit the character controller..

avatar image sdgd · Mar 02, 2013 at 02:05 PM 0
Share

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

avatar image sdgd · Mar 02, 2013 at 04:15 PM 1
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Parent rigidbody using its childrens colliders. Should it work or am I stupid? 2 Answers

Collsion without ririgibody 1 Answer

Player Falls Through Floor on Trigger 1 Answer

How do I "remove/disable" collision? 3 Answers

how to make an Explosive rigidbody trigger by another collision box 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges