Search Unity

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

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

  1. MHSalehi

    MHSalehi

    Joined:
    Oct 5, 2018
    Posts:
    6
    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

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Oh man, Unity put up a TERRIBLE example for that API. The default Ray() constructor produces a ray with direction of Vector3.zero, 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: Vector3.zero, 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

    MHSalehi

    Joined:
    Oct 5, 2018
    Posts:
    6
    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;
    6.  
    7.         // Convert vehicle 'local space' forward into 'world space' forward.
    8.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    9.  
    10.         // Draw ray forward from player.
    11.         Ray r = new Ray(player.transform.position, forward);
    12.  
    13.         // Get position at the end of the raycast.
    14.         projectionPos = r.GetPoint(distanceToProject);
    15.  
    16.         // Debugging.
    17.         if (debugging)
    18.         {
    19.             print(r.GetPoint(distanceToProject));
    20.             Debug.DrawRay(transform.parent.position, forward * distanceToProject, Color.blue);
    21.         }
    22.     }
     
    Last edited: Oct 28, 2019