Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

My Collision Detection

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

  1. blawkeyes

    blawkeyes

    Joined:
    Jun 12, 2015
    Posts:
    5
    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