Search Unity

Using Hit.point to detect collisions

Discussion in 'Scripting' started by McC1oud, Apr 21, 2015.

  1. McC1oud

    McC1oud

    Joined:
    Mar 14, 2015
    Posts:
    96
    I am trying out a new collision detection using the return vector of a raycast hit.
    I am using the following to set the y value of my falling object to the y value of "Hit.point"

    Code (CSharp):
    1. Vector3 groundLevel = floorHit.point;
    2.  
    3. if(transform.position.y <= groundLevel.y)
    4.   {
    5.  
    6.   transform.position = new Vector3(transform.position.x, groundLevel.y, groundLevel.z);
    7.  
    8.   }
    The result is that the falling object will still penetrate the ground for 1 frame and then snap to position. It is not that noticeable but I see it.

    What could I do so that the transform of the falling object absolutely does not go past the value of hit.point.
    or lessen the notice ability of the frame snap?
     
    Last edited: Apr 21, 2015
  2. zehreken

    zehreken

    Joined:
    Jun 29, 2009
    Posts:
    112
    You can calculate the position of the transform for the next frame, if the calculated position and the target position intersect or if the calculated position passes the target position, then you can snap.
     
    Last edited: Apr 21, 2015
  3. McC1oud

    McC1oud

    Joined:
    Mar 14, 2015
    Posts:
    96
    I think I get ya.

    Precalculate so on the following frame it snaps down to the surface rather than snapping up. A seamless flow of animation.

    How would I "GetNextFrame" to make that calculation?
     
  4. zehreken

    zehreken

    Joined:
    Jun 29, 2009
    Posts:
    112
    You should calculate the position by using velocity, acceleration and any other movement rules of your object.
     
  5. McC1oud

    McC1oud

    Joined:
    Mar 14, 2015
    Posts:
    96
    This Appears to be working but I'd need to make more observations.

    Code (CSharp):
    1. Vector3 oldPos = transform.position;
    2.                 Vector3 delVelocity = (velocity_XY + accel) * Time.deltaTime;
    3.                 Vector3 newPos = oldPos + delVelocity;
    4.  
    5.                 pre_CalFrame = newPos + delVelocity;
    6.                    
    7.                 transform.position = newPos;
    I created that pre_CalFrame what I assume would be the value of the next frame. Then it is used as:

    Code (CSharp):
    1. if(pre_CalFrame.y <= groundLevel.y)
    2.             {
    3.            
    4.                 transform.position = new Vector3(transform.position.x, groundLevel.y, groundLevel.z);
    5.                 isGrounded = true;
    6.                 accel.y = 0;
    7.                 velocity_XY.y = 0;
    8.  
    9.             }
    Look corrrect? Seem to be currently.
     
  6. zehreken

    zehreken

    Joined:
    Jun 29, 2009
    Posts:
    112
    I haven't tested it but it looks okay, congrats.
     
  7. McC1oud

    McC1oud

    Joined:
    Mar 14, 2015
    Posts:
    96
    Thnx mate