Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Getting the Position at the end of a Raycast (C#)

Discussion in 'Scripting' started by MHSalehi, Oct 26, 2019.

  1. MHSalehi


    Oct 5, 2018
    I've tried searching for the answer to this over the last couple days but have had no luck finding anything addressing my scenario, hence this post.

    I'd like to know if it's possible to create a raycast which has a fixed distance and get the position at the end point of the raycast (without the raycast needing to register a collision with another object).

    I have tried experimenting with
    Code (CSharp):
    1. Ray.GetPoint
    but even when trying the Unity example my console print out reads '(0.0, 0.0, 0.0)'.

    I'm sure I'm missing something basic but after spending so long without making any progress I'm hoping someone else can help point me in the right direction.
  2. Kurt-Dekker


    Mar 16, 2013
    Oh man, Unity put up a TERRIBLE example for that API. The default Ray() constructor produces a ray with direction of, so no matter how far you go down that array, you are at (0,0,0).

    Try this instead:

    Code (csharp):
    1.         Ray r = new Ray( origin:, direction: Vector3.forward + Vector3.up);
    2.         print(r.GetPoint(10)); // a point 10 units along the ray
    Edit: I posted a suggestion to the API to improve it. You can make a post too saying it was confusing to you.

    EDIT: updated the above code to used named arguments, since the two arguments have the same type.
    Last edited: Apr 23, 2023
    Randallhbarber and Spacewizard- like this.
  3. MHSalehi


    Oct 5, 2018
    Many thanks Kurt, that really helped clear things up.

    I used it to generate a projected position ahead of a player based on their current speed. Here's the code in case anyone is interested in an example:

    Code (CSharp):
    1.     // Update is called once per frame
    2.     void Update()
    3.     {
    4.         // Calculate the length the player projection needs to be using the velocity magnitude.
    5.         distanceToProject = baseProjectionDistance * player.GetComponent<AircraftInstruments>().airspeed / maxAirspeed;
    7.         // Convert vehicle 'local space' forward into 'world space' forward.
    8.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    10.         // Draw ray forward from player.
    11.         Ray r = new Ray(player.transform.position, forward);
    13.         // Get position at the end of the raycast.
    14.         projectionPos = r.GetPoint(distanceToProject);
    16.         // Debugging.
    17.         if (debugging)
    18.         {
    19.             print(r.GetPoint(distanceToProject));
    20.             Debug.DrawRay(transform.parent.position, forward * distanceToProject,;
    21.         }
    22.     }
    Last edited: Oct 28, 2019