Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 xxTheo · May 29, 2019 at 10:25 PM · collisioncollidercollision detectionraycastingkinematic

What is the cheapest way to prevent object A from going through object B

alt text Both object A and object B have a kinematic rigid body attached; Interpolate: Interpolate, Collision Detection: Continous Speculate.

I'm using onTriggerEnter to detect the collision.

The problem is both object A and object B is moving very fast. And as you probably already know the faster an object moves the greater the travel distance between frames. So in one frame, there is a good amount of distance between object A and objectB and in the next frame object A pass-through object B.

My question is, is there a way to detect collision immediately on the impact between object A and object B? I know about raycasting and based on what I learn it's expensive and it's mostly used for extremely fast moving objects like a bullet.

screen-shot-2019-05-29-at-55150-pm.png (442.6 kB)
Comment
Add comment · Show 5
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 xxTheo · May 30, 2019 at 01:48 AM 0
Share

Object A is being moved by the animation on the character's hand and object B is being moved by script. If I was to use OverlapSphere on object A, how would I use the information to stop both objects immediately when they're next to each other.

avatar image MadDevil · May 30, 2019 at 02:44 AM 1
Share

You can try using "Vector3.distance", keep checking the distance between A & B, if its less than the threshold you move them back to the closet they can get to each other i.e not overlapping each other.

avatar image xxTheo MadDevil · May 30, 2019 at 08:23 PM 0
Share

thank you for responding. Okay, say frame 10 the two objects are overlapping and frame 9 there is a moderate amount of distance between them. How can use their distance at frame 10 and frame 9 to calculate the position where they're in contact but not overlapping?

avatar image Garrett203 · May 30, 2019 at 10:14 AM 0
Share

Yes pls share the code..It will make easy for us to analyse issue and solve it

avatar image xxTheo Garrett203 · May 30, 2019 at 08:08 PM 0
Share

This is how object B is move or rather rotated alt text alt text

Object A is move by the character's animation. It parented to the character's right hand. The animation exits once Object A collide with object B. Which is done in a similar manner with on trigger but ins$$anonymous$$d of changing the rotational direction upon impact I ins$$anonymous$$d change an animation parameter from false to true

3 Replies

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

Answer by black_sheep · May 30, 2019 at 11:46 PM

Raycasts are only expensive when you use hundreds of them per frame. I'm using =~20 per frame on my project and it does not hurt performance heavily. But what you should use in this case if the collider's bounds. If the closest distance from A to the bounds of B is smaller than the radius of A, than the ball is getting through B. Luckily, unity already has the functions for us. Code would look like:

 Collider ColliderB = B.GetComponent<ColliderB>();
 float distance = colliderB.bounds.sqrDistance;
 if(distance <= A.radius){
     //the direction for going away
     Vector3 vector = A.transform.position - B.transform.position;
     //goes away by the distance to the collider + the radius of the ball
     A.transform.position += vector.normalized * (distance + A.radius)
 }
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 wyatts · May 30, 2019 at 04:17 AM

In 2018.3 Unity introduced Speculative continuous collision detection. That should help with this if you're not already using it.

Comment
Add comment · Show 2 · 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 xxTheo · May 30, 2019 at 07:16 AM 0
Share

I may have miss spelled it but I did include "Collision Detection: Continous Speculate"

avatar image xxmariofer xxTheo · May 30, 2019 at 08:06 AM 0
Share

can you share the code of how you move it? probably that way you can fix it

avatar image
0

Answer by gaggedegg · May 30, 2019 at 10:09 AM

@xxTheo

In OnTriggerEnter() there is a way to detect which object is colliding with which object by checking the tag of colliding object.

Suppose your ObjectA has tag "ObjA", you can check collision by:

private void OnTriggerEnter(Collider coll) { if(coll.CompareTag("ObjA")) { if(gameObject.name == "ObjectB") { Debug.Log("Collision happned with ObjectB "); } }

gameObject.name will give you the name of the object with which ObjectA is collided.



Hope this help :)

Comment
Add comment · Show 7 · 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 xxmariofer · May 30, 2019 at 10:17 AM 1
Share

his issue is objects not colliding because they are really fast

avatar image xxTheo xxmariofer · May 30, 2019 at 08:05 PM 0
Share

Thank you xxmariofer, I'm very grateful for every response I get but do these people actually read what I wrote or did I do a terrible job at explaining the problem.

This is how I'm moving object B alt text

alt text

Object A is move by the character's animation. It parented to the character's right hand. The animation exits once Object A collide with object B. Which is done in a similar manner with on trigger but ins$$anonymous$$d of changing the rotational direction upon impact I ins$$anonymous$$d change an animation parameter from false to true

screen-shot-2019-05-30-at-35408-pm.png (49.8 kB)
screen-shot-2019-05-30-at-35340-pm.png (113.4 kB)
avatar image xxmariofer xxTheo · May 30, 2019 at 08:59 PM 1
Share

you should get better results using forces, try changing the fixedupdate code to this

  rb.angularVelocity = speed * EulerAngleVelocity * Time.deltaTime;
  rb.maxAngularVelocity = 99999999;

this will give you more precise collisions, BUT you need to make the rigidbody not kinematic to be affected by forces

Show more comments

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

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

Restrict Rigidbody movement in the Z axis 1 Answer

Using ray cast does not produce reasonable results 1 Answer

brick does not detect collision although ball does 2 Answers

How to decrease the speed of the player by some fraction after triggering the box collider? 2 Answers

I am trying to create all elements in the scene from one script at run time. How do I detect collisions between the enemy and the coin without having to create another script for each item? 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