Search Unity

Find angle/direction of a Ray / Raycasthit

Discussion in 'Scripting' started by Ssiroo, Aug 22, 2016.

  1. Ssiroo

    Ssiroo

    Joined:
    Jun 17, 2014
    Posts:
    10
    Hello,

    I'm trying to make a Billiard game and I wanna calculate the direction on which the Cue Ball(white ball) will be moving after it hits another ball.



    As you can see I wanna calculate the angle/direction in which the RAY hits the ball and the angle/direction on which the raycast will change its direction into. I need the angle to display as a Vector3 variable so I can use it on the linerenderer(3).

    I already calculated the direction that the ball that gets hit will go.

    If you could help me on this that would be great!

    Current code:

    Code (CSharp):
    1. RaycastHit hitz;
    2.     if (Physics.SphereCast(transform.position, 0.8f, location - transform.position, out hitz, Mathf.Infinity, lmm2))
    3.     {
    4.  
    5.         lineRenderer2 = hitz.collider.GetComponentInChildren<LineRenderer>();
    6.         lineRenderer2.SetVertexCount(2);
    7.  
    8.         if (!Input.GetKey(KeyCode.Mouse0))
    9.             lineRenderer2.SetPosition(0, hitz.point);
    10.    
    11.         if (!Input.GetKey(KeyCode.Mouse0))
    12.            {
    13.                Vector3 start = hitz.point;
    14.                Vector3 end = start + (-hitz.normal * 4);
    15.    
    16.                if (lineRenderer2)
    17.                {
    18.                    if (!Input.GetKey(KeyCode.Mouse0))
    19.                        lineRenderer2.SetPosition(1, end);
    20.                }
    21.  
    22.                if(lineRenderer3)
    23.                {
    24.                  
    25. anglelel = Vector3.Angle(hitz.normal, hitz.point);
    26.                                 Vector3 cross = Vector3.Cross(hitz.normal, hitz.point);
    27.                                 if(cross.y > 0)
    28.                                 {
    29.                                     tzt = Quaternion.AngleAxis(90f, hitz.normal) * realStick.transform.forward;
    30.                                 }
    31.                                 if (cross.y < 0)
    32.                                 {
    33.                                     anglelel = -anglelel;
    34.                                     tzt = Quaternion.AngleAxis(270f, hitz.normal) * realStick.transform.forward;
    35.                                 }
    36.                                 Vector3 start2 = hitz.point;
    37.                                 Vector3 end2 = start2 + ((tzt) * 5f);
    38.                                 lineRenderer3.SetPosition(0, hitz.point);
    39.                                 lineRenderer3.SetPosition(1, end2);
    40.                }
    41.             }
    42.     }

    Thank you for your time.

    Edit:

    This part of the code has been changed to this one, currently makign some progress but still, it's not good enough.

    Before

    Code (CSharp):
    1. if(lineRenderer3)
    2.             {
    3.                 Vector3 start2 = hitz.point;
    4.                 //THIS IS WHERE I'M CURRENTLY STUCK AT
    5.                 Vector3 end2 = start2 + (hitz.point * 0.7f);
    6.                 lineRenderer3.SetPosition(0, hitz.point);
    7.                 lineRenderer3.SetPosition(1, end2);
    8.              }

    After
    Code (CSharp):
    1.  
    2.     if(lineRenderer3)
    3.     {
    4.          anglelel = Vector3.Angle(hitz.normal, hitz.point);
    5.          Vector3 cross = Vector3.Cross(hitz.normal, hitz.point);
    6.          if(cross.y > 0)
    7.          {
    8.  
    9.               tzt = Quaternion.AngleAxis(90f, hitz.normal) *realStick.transform.forward;
    10.          }
    11.           if (cross.y < 0)
    12.           {
    13.               anglelel = -anglelel;
    14.               tzt = Quaternion.AngleAxis(270f, hitz.normal) * realStick.transform.forward;
    15.            }
    16.            Vector3 start2 = hitz.point;
    17.            Vector3 end2 = start2 + ((tzt) * 5f);
    18.            lineRenderer3.SetPosition(0, hitz.point);
    19.            lineRenderer3.SetPosition(1, end2);
    20.       }