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
1
Question by destructivArts · Jun 14, 2014 at 10:23 AM · collision detectioncollisiondetectionsweeptest

Custom Collision Detection With SweepTestAll

I hate collision detection. Its honestly the reason I started using Unity, and now I'm working on a project that because of... reasons... which I have researched thoroughly, I now have to write my own collision detection.

All of my objects are using AABB cubes. I am using SweepTest on my player to get potential collisions. I then call the function shown below on the RaycastHit SweepTestAll returns. (Note: I plan on using SweepTestAll and sorting the collisions to find the closest one that I want. The first line of the function I'm showing below is how I'm "sorting" right now but that is definitely working.)

The problem I'm having is that if I approach an object with which the player should collide from one side, he passes right through. From the other, he kind of jitters on its border.

Just a note, I call this function every FixedUpdate, which is also where I actually apply velocity to the transform of the player, and yes I do it after I call this function. (Sorry, I'm a little irritable right now.)

I need the player to stay .1f on any axis away from the collider because if he gets any closer, SweepTest wont return a hit. (Basically if they're already touching, it won't return a hit.) I've been struggling with this for two days now. Any fixes, or even a better way to do this would be absolutely wonderful!

     void HandleSweepTest(RaycastHit hitInfo){
         if(hitInfo.collider.gameObject.GetComponent<Collisions>().getCollisionType() == Collisions._CollisionType.solid){
 
             Vector3 relevantBounds = Vector3.Scale (collider.bounds.extents, (hitInfo.normal * -1));
 
             Vector3 transformedBounds = collider.transform.position + relevantBounds;
 
             Vector3 distToCol = hitInfo.point - transformedBounds;
             distToCol = Vector3.Scale (distToCol, hitInfo.normal);
 
             for(int i=0;i<3;i++){
 
                 if ((Mathf.Round(hitInfo.normal[i] * 100)/100) != 0){
 
                     float l = Mathf.Round (Mathf.Abs (distToCol[i]) * 100)/100;
                     if ((Mathf.Round (Mathf.Abs (distToCol[i]) * 100)/100) < .1f){
 
                         distToCol[i] = ((.1f * (distToCol[i]/Mathf.Abs (distToCol[i]))) + distToCol[i]);
                     }else if((Mathf.Round (Mathf.Abs (distToCol[i]) * 100)/100) > .1f){
 
                         distToCol[i] = distToCol[i] - (.1f * (distToCol[i]/Mathf.Abs (distToCol[i])));
                     }else{
 
                         distToCol[i] = 0;
                     }
                 }
 
                 distToCol[i] = Mathf.Round (distToCol[i] * 100)/100;
             }
 
             Vector3 distToMove = Vector3.zero;
             for (int i=0;i<3;i++){
 
                 if (hitInfo.normal[i] != 0){
 
                     distToMove[i] = distToCol[i];
                 }else{
 
                     distToMove[i] = velocity[i];
                 }
             }
             velocity = distToMove;
         }
     }
Comment
Add comment · Show 1
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 A_Box · Sep 13, 2020 at 07:53 PM 0
Share

Same problem here, 6 years later... has anyone found a solution?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make a certain function if an object collides with another object? Been searching it up for 2 months. 1 Answer

Understanding if an object is inside of another 1 Answer

Best way to detect precise location of a collision on mesh collider? 0 Answers

collisions will sometimes be detected and sometimes will not 1 Answer

Probably another "bullet through paper problem". Jerks, teleports, and other problems 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