How to draw ray from hit point to the center of the collider

Hello everyone,

Am trying to make a simple pool game, and for the moment i don’t want to worry about projectile simulation to know where each ball will go, however, i still need to show some “line hints” to the player, and i thought i can easily doing it like this :

  • cast a ray from the white ball position based on the desired direction of the “pool stick”
  • if the ray hit another ball, then cast a new ray from the hit point toward the center of this ball

This should be pretty straight forward but for some reason am getting this as a result :
alt text

but what i need is this :
alt text

The code am using is this :

void CheckForHits ()
    {
      Vector3 fwd = transform.TransformDirection (new Vector3 (Vector3.left.x, Vector3.left.y + 270, Vector3.left.z));
      //+270 because the pool stick is because i need to invert the line cause am hiting the ball from behind
 
       RaycastHit objectHit;
 
       if (Physics.Raycast (transform.position, fwd, out objectHit, 50)) {
                   if (objectHit.collider.name.Contains ("ball")) {

                     Debug.DrawRay (objectHit.point, objectHit.collider.transform.position, Color.green);
                        }
             }
  }

Thank you

DrawRay() takes a position and direction, so the code should be:

 Vector3 dir = (objectHit.transform.position - objectHit.point).normalized;
 Debug.DrawRay (objectHit.point, dir  * 10.0f, Color.green);

That will draw a 10 unit line starting at the hit point and passing through the center of the collider.