Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

My Collision Detection

Discussion in 'Physics' started by blawkeyes, Oct 13, 2018.

  1. blawkeyes

    blawkeyes

    Joined:
    Jun 12, 2015
    Posts:
    4
    Code (CSharp):
    1.  
    2.  
    3.     private void CorrectOverLaps()
    4.     {
    5.         //MSPaint code
    6.         //Dot[(p_target-hit.point),hit.normal] = s;
    7.         //s = (s / 2) - hitboxradius;
    8.         //ptarget = ptarget + s * hit.normal
    9.             foreach (RaycastHit hit in Physics.SphereCastAll(this.gameObject.transform.position,hitBoxRadius-hitBoxPadding,(targetPos-this.gameObject.transform.position),((targetPos-this.gameObject.transform.position).magnitude+hitBoxPadding),groundLayer))
    10.             {
    11.                 dotproduct = 0;
    12.                 dotproduct =  Vector3.Dot((targetPos-hit.point), hit.normal);
    13.                     //If we are behind the plane solve our position
    14.                     if(dotproduct<hitBoxRadius+hitBoxPadding)
    15.                     {
    16.                         if(dotproduct<0)
    17.                         {
    18.                             dotproduct = (dotproduct / 2) - hitBoxRadius-hitBoxPadding;
    19.                             targetPos = targetPos - dotproduct * hit.normal;
    20.                         }
    21.                         else if (dotproduct > 0)
    22.                         {
    23.                              dotproduct = (hitBoxRadius+hitBoxPadding)-(dotproduct);
    24.                              targetPos = targetPos + dotproduct * hit.normal;
    25.                         }
    26.                     }
    27.             }
    28.     }
    29.  
    30.  
    This is my custom overlap detection without using rigidbodies or colliders. I like moving my characters with set position.
    This code works and has very smooth detection but I was hoping someone could help me clean it up. to be more readable. My only qualm at the moment is that the character can some times jitter ever so slightly if you run him into specific angles. I was thinking maybe if the target pos and the current position are to small of a difference then dont set the current position to the target position at all because its not neccessary.

    Thoughts?
     
    Last edited: Oct 13, 2018