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 fafnir · Jan 30, 2014 at 11:14 PM · 2drigidbodyvelocity

Problems reading Rigidbody velocity magnitude

I'm trying to make a boss-level for a 2D game, in which the character gets shot out of a cannon and has to hit the boss. In the boss-script I'm trying to measure the force with which the character hits it, so that I can figure out if it was hard enough for the boss to take damage. My code for this is

 void OnCollisionEnter2D(Collision2D col) {
     if (col.gameObject.name == "CannonAmmo"/* && col.gameObject.rigidbody2D.velocity.magnitude >= damageBound*/) {
         Debug.Log("Was hit with force " + col.gameObject.rigidbody2D.velocity.magnitude);
     }
 }

The problem is that the debug-message prints a velocity of anything between about 2 to about 30, for almost the exact same situation :P Any ideas why this might be? As it is now, it's obviously unusable.

Fafner

Comment
Add comment · Show 3
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 robertbu · Jan 31, 2014 at 06:25 AM 1
Share

Untested idea: Save the velocity.magintude each frame. Then use the velocity.magnitude from the previous frame (before the collision). You may have to test and play to get the order right.

avatar image supernat · Jan 31, 2014 at 06:59 AM 1
Share

I'm with robertbu on this one. You probably need to take the velocity at a point away from the end point. I don't know the internals of the Unity collision system, but they might adjust the velocity for the impact of the collision before sending the OnCollision event (which seems wrong).

Is there any reason you can't use the initial velocity? It seems that you should be in control of the shot out of the cannon and its velocity and thereby able to use that velocity as an indicator of how hard the player hits the boss, unless you have something in the player's path that can slow him down other than gravity.

avatar image fafnir · Feb 01, 2014 at 11:41 AM 0
Share

Thanks, I'm gonna try this! :) supernat, I guess you're right, this might be too complicated for what I'm trying to achieve :P

1 Reply

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

Answer by Owen-Reynolds · Jan 31, 2014 at 04:53 PM

In this case, you could probably use collision2D.relativeVelocity.magnitude. It will also count the boss's speed, but that might be more correct (hitting while it moves away would be a little softer.)

Tested, just now: OnCollisionEnter now happens after the physics part is resolved, which seems more correct to me. Often I want to tweak the bounce, like a wall that only sends things sideways, or removes all spin. If OnCollision is called pre-physics, this is way hard and confusing (a 1-frame delay coroutine?)

As robertu points out, maintaining "trailing" vars, if you want more pre-collision stats, isn't that much work.

Ideally, there would be an OnPreCollisionEnter function (but probably just slows down the framework, for virtually no gain.)

Comment
Add comment · Show 1 · 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 fafnir · Feb 01, 2014 at 11:40 AM 0
Share

Thanks, I was thinking about that, taking the boss' velocity into account as well. I'm gonna try out both suggestions:)

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AddForce vs Velocity issues with Rigidbody2D 2 Answers

Velocity Movement & Physics Interactions by Rigidbody2D 0 Answers

C# Rotate velocity issue 1 Answer

ball breaks through walls 1 Answer

Velocity powered rigidbody on a moving platform without parenting. 3 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