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 filler03 · Apr 30, 2012 at 07:28 AM · collisionrigidbodyvelocitymagnitude

Rigidbody magnitude comparison is not working correctlly?

i want to compare the magnitudes of two rigidbodies and destroy the one that is moving slower when they collide. I do this by comparing the magnitudes of the 2 rigidbodies. It works fine most of the time but sometimes the wrong object gets destroyed, and when this happens one of the objects is completely at rest and the other is moving, so there is no way that the magnitudes would be equal. The code looks likes this:

 function OnCollisionEnter(collision : Collision) {
 
     
 
     if(collision.gameObject.tag == "projectile") {
     
     
         if(collision.rigidbody.velocity.magnitude < rigidbody.velocity.magnitude)     {
             
               collision.gameObject.GetComponent(ProjectileScript).destroyMe(player);
             
         }
         
         
         
     }

any ideas??

Comment
Add comment · Show 8
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 Kryptos · Apr 30, 2012 at 07:31 AM 0
Share

Can you show us the code for the destroy$$anonymous$$e method?

Also, not related but small perofrmance improvement, use sqr$$anonymous$$agnitude when you just want to check a difference:

 if(collision.rigidbody.velocity.sqr$$anonymous$$agnitude < rigidbody.velocity.sqr$$anonymous$$agnitude)
avatar image Seth-Bergman · Apr 30, 2012 at 07:43 AM 0
Share

start with a Debug Log. As in :

Debug.Log(collision.rigidbody.velocity.magnitude); Debug.Log(rigidbody.velocity.magnitude);

that may provide a clue. also, the mass will affect magnitude along with speed, so if the objects have different masses, that could affect it.

avatar image Lo0NuhtiK · Apr 30, 2012 at 07:55 AM 0
Share

Also might not be working as expected because they've already collided when it checks the velocity ; so the velocity has already changed, and isn't what it was before impact.
e.g.
before Impact : Obj01 speed = 100.0 , and Obj02 speed = 500.0 ; ... you want Obj01 to die here
... but...
After Impact : Obj01 got a kickstart from highspeed Obj02 and it's speed now = 50 heading in the other direction, and Obj02 bounced off the other one fairly hard and it's speed ended up decreasing to 35 so it dies ins$$anonymous$$d.
Not sure if that's what's going on here, but that's the first thing that came to $$anonymous$$d. Need the speeds of them from right BEFORE they hit each other, not afterward ; and not sure if OnCollisionEnter would call and calculate quite fast enough all the time to catch the speed before it changed. $$anonymous$$ight be why it works for you most of the time, but not always.

avatar image Eric5h5 · Apr 30, 2012 at 07:57 AM 1
Share

$$anonymous$$ass doesn't affect magnitude or speed at all, and it would be really wrong if it did. Velocity is speed/direction only, magnitude is simply how much speed there is. The only affect mass has is how much force it takes to get to a given speed.

avatar image Seth-Bergman · Apr 30, 2012 at 08:02 AM 0
Share

duh, my mistake, of course. Thinking of the wrong thing

Show more comments

2 Replies

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

Answer by Kryptos · Apr 30, 2012 at 03:01 PM

The magnitude inside OnCollisionXXX is the magnitude after the collision has been processed by the physics engine.

If you want the magnitude before the collision, you will have to save it in a variable and update it each FixedUpdate (in both scripts):

 var prevSqrMagnitude : float;

 void FixedUpdate()
 {
     prevSqrMagnitude = rigidbody.velocity.sqrMagnitude;
 }

Then, in the script with the OnCollisionEnter function:

 function OnCollisionEnter(collision : Collision) {
     if(collision.gameObject.tag == "projectile") {

         var projectile : ProjectileScript = collision.gameObject.GetComponent(ProjectileScript);
         if(projectile.prevSqrMagnitude < prevSqrMagnitude ) {
             projectile.destroyMe(player);
         }
     }
 }
Comment
Add comment · 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
0

Answer by aldonaletto · Apr 30, 2012 at 03:03 PM

@Lo0NuhtiK may be right: the velocities may have been changed by the collision. You could store the velocities in FixedUpdate in both objects and compare them:

var lastVel: float = 0;

function FixedUpdate(){ // this function must exist in both scripts! lastVel = rigidbody.velocity.magnitude; }

function OnCollisionEnter(collision : Collision) { if (collision.gameObject.tag == "projectile") { var otherScript = collision.gameObject.GetComponent(ProjectileScript); if (otherScript.lastVel < lastVel){ // compare the velocities prior collision otherScript.destroyMe(player); } } }

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

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

9 People are following this question.

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

Related Questions

void OnCollisionEnter(Collider other) { if (Rigidbody.velocity.magnitude > 5) {...} - In "Rigidbody.velocity.magnitude" asks to give link to the object??? 2 Answers

Collision issues with changing direct velocity 0 Answers

Updating Velocity for a Rigidbody Collision 1 Answer

Rigidbody.velocity.magnitude only returns value of the Y axis 1 Answer

rigidbody It's going up and bounces when is moving 1 Answer


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