Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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