Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by troyle · May 19, 2017 at 06:38 PM · physicsrigidbodyvelocity

Overriding OnCollisionEnter function

I want to implement a function that does a lossless reflection of an object to be used in a billiard game. I am trying to override the OnCollisionEnter function in the collision of objects.

 private void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.GetComponent<Rigidbody>())
            return;
 
         Vector3 inVec = rb.velocity;
         Vector3 normal = collision.contacts[0].normal;
 
         if (normal == Vector3.up) return;
         if (Vector3.Dot(inVec.normalized, normal) < 0)
             return;
 
         Vector3 outVec = inVec - 2 * Vector3.Dot(inVec, normal) * normal;
         rb.velocity = outVec;
     }

This is the code I am using, but when I set the rigidbody (rb) velocity to outVec, it doesn't change the actual value (next frame it is unchanged)

How can I change the velocity so it actually affects the rigidbody?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Matthewj866 · May 19, 2017 at 06:47 PM

I am going to assume there is a reason for not letting the physics engine calculate the velocity if it's just for bouncing off other colliders.

The problem is not in the velocity assignment - the issue is the values you are putting in. This will only work if this collides with something else, rather than anything colliding into this object when it is stationary.

You need to adjust the maths and input here to take both objects' velocity into account if you're trying to manually assign a velocity based on a collision.

Comment
Add comment · Show 4 · 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 troyle · May 19, 2017 at 09:18 PM 0
Share

The reason is that when the balls hit the side, they roll along the side and not bounce off. I cannot get them to bounce consistently with a physics material. So I have a physics material on the balls that is used for bouncing off balls and my script is for bouncing off the walls. The issue with this is that in some circumstances, the ball bounces off the wall and then renters the collision and bounces back again doing a kind of ping pong/vibrate effect. The script was to fix this but it doesnt work.

avatar image Matthewj866 troyle · May 20, 2017 at 03:22 AM 0
Share

Your original question was missing this crucial information - the issue you want solved only now makes it apparent in this comment.

I don't think we have enough information to fix colliders re-colliding with the ping-pong effect you've described to be honest, so as an educated guess if it's getting "stuck" in the other collider causing this effect you might want to cap the maximum velocity of objects so that they can't slip past the physics updates.

If this is not the issue, can you please provide us with more information?

avatar image troyle Matthewj866 · May 20, 2017 at 11:34 AM 0
Share

Well I thought that when I override the OnCollisionEnter function it would only do my function, but it appears that it doesnt. I dont know if it is skipping past the update because it triggers the enter, exit then enter again so its like a double hit.

Show more comments
avatar image
0

Answer by BroncoBilli · Oct 18, 2018 at 05:08 AM

I'm not sure anybody BOTHERED to answer his question. I have the same question. Why is it when I assign a velocity to the rigidbody in OnCollisionEnter, it doesn't affect my object. It's as if the physics engine overwrites it in the very next step. There should be a "collision.handled" event where if you set it to true, the physics engine leaves the calculation alone in the next step in the physics chain.

I will reask the question directly: Why, in OnCollisionEnter, if I set my object's rigidbody's velocity to some ludicrous #, does that value not take effect in the game, and my object just sort of moves a tiny bit. Unity is overwriting this value. why?

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 BroncoBilli · Oct 18, 2018 at 05:08 AM 0
Share

I guess that should have been a comment. whoops.

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

153 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 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

Why Does Bounciness Affects Rigidbody Velocity? (SOLVED) 0 Answers

Counteracting rigidbody velocity using AddForce? 0 Answers

Rigidbody.velocity Movement code produce wildly different gravity interactions 0 Answers

AddForce instead of modyfing Velocity 4 Answers

How to set velocity from connected Rigidbody? 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