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 zafadadynamic · Mar 23, 2014 at 07:45 PM · collisiononcollisionenterphysicmaterialbounciness

PhysicMaterial Property Change Not Effective If Change During Collision

The problem is that when the collision occurs, the angle of collision is calculated and then the bounciness changes. I see in the inspector that it does actually change like it's supposed to, but alas, no bouncy.

Well, here's the code. I've tried a few workarounds that you can see with the enabled = true/false, and also with PrevCollisionObject. With PrevCollisionObject, that was created because I thought maybe Unity was accidentally causing multiple collision calls. The code's a mess, I know, but try to look past that. First run trying to figure things out is usually duct taped together anyway.

The collider.enabled part was something I found while tirelessly looking through Unity Answers which had no effect.

Alright, so to clearly state things and put everything in a nutshell - I need the bounciness property to change on collision (which it does) and then actually bounce using the new bounciness value.

Thanks in advance and I'm REALLY sorry this is so messy :)

 #pragma strict
 
 var Ball : Transform;  //The ball.
 var ObjectDirection : GameObject;  //Object used that always points in direction of ball movement. 
 var RecPreCollisionPosition : int;  //Record PreCollisionPosition every nth frames.
 var MaxAllowedAngle : int;  //Angle of impact that allows bounciness.
 var Bounciness : float; //Bounciness applied when all requirements are met. 
 
 static var PrevCollisionObject : GameObject;  //Previous object hit.  Shared across all scripts. 
 internal var PreCollisionPosition : Vector3;  //Position before collision detection.
 internal var PostCollisionPosition : Vector3;  //Position after collision detection.
 internal var RecPreCollisionPositionIt : int;  //RecPreCollisionPosition iterator.
 internal var AngleOfIntersection : float;  //Angle of intersection between ball and forward hit point.
 
 function Start () {
 
     PreCollisionPosition = Ball.position;  //Records position on hit just in case the ball is near the wall and position recording does not record an effective position.
 }
 
 function FixedUpdate () {
 
     if (RecPreCollisionPositionIt == RecPreCollisionPosition) {
     
         PreCollisionPosition = Ball.position;  //Records position of ball during this frame.
         RecPreCollisionPositionIt = 0;  //Resets frame recording iterator.
     } else if (RecPreCollisionPositionIt < RecPreCollisionPosition) {
     
         RecPreCollisionPositionIt++;  //Increments frame recording iterator.
     }
 }
 
 function OnCollisionEnter (collision : Collision){
 
     PostCollisionPosition = Ball.position;  //Saves ball position on collision.
     
     AngleOfIntersection = Mathf.Abs(Vector3.Angle(collision.contacts[0].normal, (PostCollisionPosition - PreCollisionPosition)));  //Angle of intersection.
     
     if (AngleOfIntersection < MaxAllowedAngle) {
     
         PrevCollisionObject = gameObject;  //This will store the game object that satisfied the requirements.  This will then be compared to the else statement to prevent wrong bounciness changes. 
         
         gameObject.collider.sharedMaterial.bounciness = Bounciness;  //Sets bounciness if angle of intersection is less than specified angle.
         gameObject.collider.enabled = false;
         gameObject.collider.enabled = true;    
     } else if ((PrevCollisionObject != gameObject) && (AngleOfIntersection >= MaxAllowedAngle)){
         
         gameObject.collider.sharedMaterial.bounciness = 0; 
         gameObject.collider.enabled = false;
         gameObject.collider.enabled = true;
     }
     
     /*//Ball.rigidbody.Sleep();
         
     //Raycast from ball position to direction.
     var Hit : RaycastHit;
     
     if (Physics.Raycast (ObjectDirection.transform.position, ObjectDirection.transform.TransformDirection (-Vector3.forward), Hit)) {
     
         
     }*/
 }

 
Comment
Add comment · Show 2
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 zafadadynamic · Mar 23, 2014 at 09:58 PM 0
Share

So I ended up trying to change the entire material too, and I can see in the inspector that the material changed. I still have the same problem though. I even rearranged the code like this:

 gameObject.collider.enabled = false;
 gameObject.collider.material = Default$$anonymous$$aterial; 
 gameObject.collider.enabled = true;

It feels like it should be such a simple thing to fix.

avatar image zafadadynamic · Mar 24, 2014 at 06:03 PM 0
Share

Alright, well I was thinking about this logically and I thought this from the beginning, but I was somewhat attempting to delude myself into thinking I was wrong.

This probably will never work because the ball already collided with the object, so being that the collision is done, it can't exact institute the new values until it's hit the next time.

The reason I got confused is because it seems like others got it to work the way I wanted it to.

I could always just use raycasting, but the ray would miss the tail ends of a collision. I was maybe even thinking of maybe the curves smoother with more colliders where then I could just adjust the bounciness level to work perfect. Problem with that is that Unity sees a collision in between two points as a line, which it should, but it will never stick to a curve when the colliders have bounciness greater than zero.

Eh, if anyone has a solution to this, let me know.

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

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

Delayed Collisions Bug (includes video demonstration) 1 Answer

Boo & Collision 1 Answer

Why is my OnCollisionEnter function working for enemies and not my player? 1 Answer

Checking Object Collision Without Script 1 Answer

Ammo crate collision 2 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