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 abcd_lsg · Jul 17, 2020 at 07:39 AM · rigidbody-collisionrigidbody physics

Cancel the torque resulting from a collision (found whats wrong, don't know how to fix it)

Hello

------I'm currently designing a RigidBody controller from scratch and need to handle collisions differently based on where they hit my object. Specficially, i need to be able to cancel the torque that results from collisions on specific parts of my character. I've figured i could try and do this by calculating torque manually and adding it back into the character for every onCollisionEnter and onCollisionStay, with the impulse and point members of the Collision class, and while this does make a massive difference in the ammount of torque my character experiences from the collisions in question, its not perfect, and sometimes it will even create a nonzero cancel torque on a collision that doesn't contribute any torque (examples later in text)

 private void antiTorque(Collision collision)
 {
     Vector3 counterTorque; // will be used to cancel the torque resulting from the collision
     Vector3 r; // will represent the vector that represents the colision point relative to the players center of mass
     for (int i = 0; i < collision.contacts.Length; i++)
     {
         r = collision.GetContact(i).point - player.transform.position;
         if (Vector3.Dot(r, player.transform.Up) < .7f)
         {
             groundedBufferCurrent = .1f;
             counterTorque = Vector3.Cross(collision.impulse, (collision.GetContact(i).point - player.transform.position)); // calculates the negative of the torque resultant from the collision
             counterTorque -= Vector3.Dot(player.transform.up, counterTorque) * player.transform.up; // negates any torque that would cause the player to turn left/right
             player.AddTorque(counterTorque, ForceMode.Impulse);
         }
     }
 }



---------This is roughly the code i originally started working with, i have since added a decent amount to it , but the problem started here, specifically, the r vector calculation appears to be off, because even when i'm on completely flat ground and standing straight up (which would mean r should be (0,-1,0)) r will debug.log output something like (0,-1,0.1459) if i'm walking in the z direction, or (-0.0925,-1,0) if i'm walking in the negative x direction, or some combination of them if i'm walking in a diagonal direction. And when i jump, it will return things like (0,-1.165,0) which shouldn't be possible because the lowest point on my collider should be at most 1 unit beneath my players center of mass.

---------Like i said earlier, this code mostly works great, i originally wrote it so that my character wont lean when trying to walk up slanted surfaces, and it solves that almost perfectly. Likewise, (before i wrote my stepUp function) if my character hit a step without my antiTorque, my character would lean jerk sharply in the direction the step impedes, and with this antiTorque function, he still lean jerks in the same direction, but ALOT less.

---------And after investigation and finding the r vector to be off, this all makes sense. For example, with the standing straight up and moving without any friction example, the collision itself should contribute a zero torque, r X F = (0,-1,0) X (0,F,0) = (0,0,0) for all possible force magnitudes F, but if r is off by any ammount in the x or z direction, then my code will calculate a nonzero antiTorque and cause my player to lean.

--------- So my question is this, does anyone here know a decent way to get the r vector from the collision class without calculating it manually? I've also tried using the transform.InverseTransformPoint() function to calculate the r vector but it had the exact same error, if not, does anyone know of a way to work around it?

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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Do rigid bodies with force add force to other objects? 0 Answers

Problem with Rigidbody control script! (collision with perpendicular walls problem) 1 Answer

Wall jump, jump movement not working 0 Answers

'height' is not a member of 'UnityEngine.Collider'. 1 Answer

Rigidbody is entering steep slopes on terrain 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