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
1
Question by ThomasQ · Dec 17, 2011 at 11:50 PM · rigidbodyvelocityragdoll

Add velocity / forces from object to instantiated ragdoll

Hi All!

I want some NPC's to turn into ragdolls when colliding with the player. For some reason I can't seem to give the instantiated ragdoll the same velocity / force, as the NPC it instantiated from, and it just stops and slides to the ground.. Did I script it wrong? Or did I take a whole wrong aproach to giving the Ragdoll velocity when instantiated?

     function OnCollisionEnter(collision : Collision)
     {
         if(collision.gameObject.tag == "Player"){
          ragdoll = Instantiate(ragdoll, transform.position, transform.rotation);
 
          colliders = ragdoll.GetComponentsInChildren(Collider);
            for (var col : Collider in colliders)
                 {
                   Physics.IgnoreCollision(col, collider);
                 }
     
        rigidbodies = ragdoll.GetComponentsInChildren(Rigidbody);
                for (var child : Rigidbody in rigidbodies) 
                 {
                 child.rigidbody.velocity = rigidbody.velocity;
                 child.angularVelocity = rigidbody.angularVelocity;
                 //child.rigidbody.AddForce (Vector3.up * 1000);
                 }    
         Destroy(gameObject);
         }
     }

the rigidboy.AddForce at the end is there for test purposes, and works in the current setup. Setting the child's velocities doesn't seem to work at all though.. If been searching for a while now, since I'm guessing it's a common problem, but I can't seem to find anything usefull on the subject...

Thanks in advance!! I've been struggling with this for days, any help apriciated!

Comment
Add comment
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

1 Reply

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

Answer by aldonaletto · Dec 18, 2011 at 03:09 AM

That's an interesting question, thus I decided to test this script. It worked fine, but with some peculiarities: when the NPC collides with a static or heavy thing, its velocity falls to almost zero - and such small velocity is transfered to the ragdoll, what makes it just fall down; on the other hand, if it collides with some light object, its rigidbody.velocity doesn't change too much, and the ragdoll parts continue moving as the original object.
I tried a simple trick: I stored the rigidbody velocity and angularVelocity in two variables during FixedUpdate, and copied these values to the ragdoll parts in OnCollisionEnter, and it worked fine! It seems that FixedUpdate occurs before the collision detection, thus when OnCollisionEnter happens the old velocity is safely stored in those variables.
That's the trick:

var lastVel: Vector3; // save the rigidbody velocities in var lastAngVel: Vector3; // these variables

function FixedUpdate(){ lastVel = rigidbody.velocity; lastAngVel = rigidbody.angularVelocity; }

function OnCollisionEnter(collision : Collision) { ... for (var child : Rigidbody in rigidbodies) { child.velocity = lastVel; // use the saved velocities child.angularVelocity = lastAngVel; }
...

Comment
Add comment · Show 2 · 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 ThomasQ · Dec 18, 2011 at 04:38 AM 0
Share

Thanks!! It works like a charm!! Plus I learned the difference between Update & Fixed Update, turns out I didn't know that very well.. You're well worth youre 9.7$$anonymous$$ points! :D

avatar image ThomasQ · Dec 18, 2011 at 03:23 PM 0
Share

Hmmm, looks like I was a little bit premature :P Although your code did help me move the ragdoll, there's still a little problem. I posted a new question here, if you'd like to check it out:

http://answers.unity3d.com/questions/196521/instantiated-ragdoll-bounces-back-for-1-frame.html?viewedQuestions=196347&viewedQuestions=179365

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

how to limit horizontal input when jumping 1 Answer

Rigidbody cube randomly stops moving? 1 Answer

AddRelativeForce not working correctly 1 Answer

Simulate a ball bouncing off of a linecast? 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