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 iamsteele · Jul 17, 2014 at 01:12 AM · physicsoncollisionenter

Collider collides, triggers OnCollisionEnter, then bounces, before destroying itself

essentially i have a OnCollisionEnter function call Destroy on the object, yet the object is hitting and bouncing off of the collider it's calling the OnCollisionEnter on... shouldn't it be calling that as soon as it knows its going to bounce.. and destroying itself before it has a chance to bounce away?

looking for a fix to this, before I am forced to switch over to ray/spherecasting

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 Saad_Khawaja · Jul 17, 2014 at 08:22 AM 0
Share

Can you post the code here that you're currently using?

avatar image iamsteele · Jul 17, 2014 at 01:10 PM 0
Share

it's just the stock code from the ulink SnowBox demo, if you have that project you can see the whole spectrum of the issue. It bounces off of anything it collides with before being destroyed a milisecond later. I did debug.log(Time.time) in the first line of the OnTriggerEnter() and OnCollisionEnter() and in OnDestroy() and the times match up, so that proved that the moment it gets the message, the ball has already hit the collider and bounced off of it before sending the OnCollisionEnter....

2 Replies

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

Answer by iamsteele · Jul 17, 2014 at 01:19 PM

After posting this question, further research lead me to many questions about the same thing, but the one that clearly states my question is this, and as such, I will implement my own code to detect the collision before it happens... because OnCollisionEnter is called a frame later than it "should" be.

http://answers.unity3d.com/questions/320069/oncollisionenter-before-physic-simulations.html

EDIT: here's the code I implemented, basically a refactoring of the DontGoThroughThis.cs on the wiki.

//variables for raycasting in fixed updated private float minimumExtent; //private float partialExtent; private float sqrMinimumExtent; private Rigidbody myRigidbody;

 //optimized declarations for fixedupdate
 private float movementSqrMagnitude;
 private Vector3 movementNextStep;
 private float movementMagnitude;
 private RaycastHit hitInfo;
 Vector3 currentPosition = new Vector3();
 Vector3 newPosition = new Vector3();
 Vector3 nextPosition = new Vector3();
 
 //initialize values 
 void Awake() 
 { 
     myRigidbody = rigidbody; 
     minimumExtent = Mathf.Min(Mathf.Min(collider.bounds.extents.x, collider.bounds.extents.y), collider.bounds.extents.z); 
     //partialExtent = minimumExtent * (1.0f - skinWidth); 
     sqrMinimumExtent = minimumExtent * minimumExtent; 

     myRigidbody.velocity = transform.forward * SPEED;
     myRigidbody.angularVelocity = new Vector3(SPIN, SPIN, SPIN);
     
     Destroy(gameObject, timeToLive);
 } 

 void FixedUpdate() 
     { 
         //have we moved more than our minimum extent? 
         currentPosition = myRigidbody.position;
         newPosition.x = currentPosition.x + myRigidbody.velocity.x * Time.deltaTime;
         newPosition.y = currentPosition.y + myRigidbody.velocity.y * Time.deltaTime - 0.5f * -9.81f * Time.deltaTime * Time.deltaTime;
         newPosition.z = currentPosition.z + myRigidbody.velocity.z * Time.deltaTime;
         //newVelocity.y = oldVelocity.y - gravity * timeDelta;
         nextPosition.x = newPosition.x + myRigidbody.velocity.x * Time.deltaTime;
         nextPosition.y = newPosition.y + myRigidbody.velocity.y * Time.deltaTime - 0.5f * -9.81f * Time.deltaTime * Time.deltaTime;
         nextPosition.z = newPosition.z + myRigidbody.velocity.z * Time.deltaTime;
         //nextVelocity.y = newVelocity.y - gravity * timeDelta;
 
         movementNextStep = nextPosition - newPosition; 
         movementSqrMagnitude = (nextPosition - currentPosition).sqrMagnitude;
         
         if (movementSqrMagnitude > sqrMinimumExtent) 
         { 
             movementMagnitude = Mathf.Sqrt(movementSqrMagnitude); 
 
             //check for obstructions we will have hit
             //Debug.DrawLine (currentPosition, nextPosition);
             if (Physics.Raycast(currentPosition, movementNextStep, out hitInfo, movementMagnitude, layerMask.value)) {
                 CustomOnCollisionEnter(hitInfo.collider);
             }
         } 
     }
 
     //For visual affects only. Actual collisions are calculated on the server.
     void CustomOnCollisionEnter(Collider colliderInfo)
     {
         //Debug.Log ("CustomCollisionEnter hit at "+Time.time+" with"+colliderInfo.name);
         if (hitSplatter != null) {
             Instantiate (hitSplatter, transform.position, Quaternion.identity);
             Destroy (gameObject);
         }
     }
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 malu · Jul 17, 2014 at 09:10 AM

You can set the rigidbody kinematic to true for the object that just entered the trigger. Maybe you have to tweak your trigger area for this.

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 iamsteele · Jul 17, 2014 at 01:12 PM 0
Share

Thanks for the reply, but this question more pertains to the fact that I have physics ti$$anonymous$$g issues where the OnCollisionEnter method is called AFTER the physics engine detects a collision and steps the ball through the steps of bouncing off the target it hit and moving forward as far as it would during the normal physics step.

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

24 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

Related Questions

How to get OnCollisionEnter working with Terrain. JavaScript 2 Answers

2D 360 degress platformer example needed 0 Answers

Optimizing C# Code and physics 2 Answers

Fungus C# ExecuteBlock() won't execute when triggered by collision or trigger enter…! 0 Answers

Location of RigidBody2D on collision 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