Search Unity

Resolved Possible to raycast between 2 objects, but then continue a set distance?

Discussion in 'Scripting' started by RealMcCoyGames, Jul 15, 2021.

  1. RealMcCoyGames

    RealMcCoyGames

    Joined:
    Jul 23, 2018
    Posts:
    65
    So if I raycast from one object to another but then instead of stopping at that object the ray continues in that direction for say 1f. Then return that position for use elsewhere.

    Thanks!
     
  2. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    Is there a reason why you can't just calculate that using the transform of source and the object you hit?
     
    RealMcCoyGames likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Don't really need a raycast for that, it's just some Vector math.
    Code (CSharp):
    1. Vector3 GetExtendedPosition(Vector3 from, Vector3 to, float extraDistance) {
    2.   Vector3 direction = (to - from).normalized;
    3.   return to + (direction * extraDistance);
    4. }
    5.  
     
    RealMcCoyGames and adamgolden like this.
  4. RealMcCoyGames

    RealMcCoyGames

    Joined:
    Jul 23, 2018
    Posts:
    65
    Yea that makes sense, like use the objects to get the direction, then just raycast out in that direction 1f. Thanks.

    Ah awesome thanks. Will give it a try!