Search Unity

Contact Point and SphereCast hit point is not the same

Discussion in 'Physics' started by alexxjaz, Oct 24, 2019.

  1. alexxjaz

    alexxjaz

    Joined:
    Sep 18, 2018
    Posts:
    45
    Hello. So I have a Sphere that will move in a scenario, like an Arkanoid, the problem is that sometimes the point where the collider hits and the SphereRaycast hit point is not the same.

    For example in this image the ray sais that it should go between the blocks, but what will happen (and what it should) its that it will get reflected to the bottom.

    https://imgur.com/a/Vf8LZqK


    Ball Controller Script:

    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    3.         lastVelocity = rb.velocity;
    4.         //Hacemos que mantenga una "velocidad" constante
    5.         rb.velocity = rb.velocity.normalized * (ballSpeed);
    6.         if(rb.velocity == Vector3.zero)
    7.         {
    8.             rb.velocity = speedVelocity;
    9.         }
    10.     }
    11.  
    12.     void OnCollisionEnter(Collision col)
    13.     {
    14.         ContactPoint cp = col.contacts[0];
    15.         rb.velocity = Vector3.Reflect(lastVelocity, cp.normal);
    16.  
    17.     }
    Prediction Script: (In the ball too)

    Code (CSharp):
    1. public class BallProyection : MonoBehaviour
    2. {
    3.     private Rigidbody rb;
    4.     [SerializeField] private LayerMask layerMask;
    5.  
    6.     [SerializeField] private int maxReflectioncount = 5;
    7.     [SerializeField] private float maxStepDistance = 200;
    8.  
    9.  
    10.     private void Start()
    11.     {
    12.         rb = GetComponent<Rigidbody>();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         Vector3 position = transform.position;
    18.         Vector3 direction = rb.velocity.normalized;
    19.  
    20.         RaycastHit hit;
    21.         if (Physics.SphereCast(position, 0.125f, direction.normalized, out hit, maxStepDistance, layerMask))
    22.         {
    23.             direction = Vector3.Reflect(direction, hit.normal);
    24.             position = hit.point;
    25.             Debug.Log("El hitpoint es: " + hit.point);
    26.         }
    27.     }
    28.  
    29.     private void OnDrawGizmos()
    30.     {
    31.         Gizmos.color = Color.red;
    32.         Gizmos.DrawWireSphere(this.transform.position, 0.125f);
    33.  
    34.         DrawPredictedReflectionPattern(this.transform.position, rb.velocity, maxReflectioncount);
    35.     }
    36.  
    37.     private void DrawPredictedReflectionPattern(Vector3 position, Vector3 direction, int reflectionsRemaining)
    38.     {
    39.         if (reflectionsRemaining == 0) return;
    40.         Vector3 startingPosition = position;
    41.  
    42.         RaycastHit hit;
    43.         if(Physics.SphereCast(position, 0.125f, direction.normalized ,out hit, maxStepDistance, layerMask))
    44.         {
    45.             direction = Vector3.Reflect(direction, hit.normal);
    46.             position = hit.point;
    47.         }
    48.         else
    49.         {
    50.             position += direction * maxStepDistance;
    51.         }
    52.  
    53.  
    54.         Gizmos.color = Color.yellow;
    55.         Gizmos.DrawLine(startingPosition, position);
    56.  
    57.         DrawPredictedReflectionPattern(position, direction, reflectionsRemaining - 1);
    58.     }
    59. }