Search Unity

raycast with an offset at hit.point? Easy for someone!

Discussion in 'Editor & General Support' started by Gibbonuk, Jun 22, 2016.

  1. Gibbonuk

    Gibbonuk

    Joined:
    Dec 10, 2013
    Posts:
    175
    Hi, Im raycasting which then gives me a hit.point, all good. However, what i want is to give the hit.point an offset TOWARDS the origin of the raycast. The ray can be cast from any angles, which is the bit thats confusing me. I need the offset to always be a certain disnce back towards its origin.

    Thanks
    Andy
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Take the hit.point and subtract the normalized ray direction times the offset distance you want.

    Code (CSharp):
    1. float offset = 5; // <-- put any number here
    2. Vector3 offsetPoint = hit.point - rayDirection.normalized * offset;
     
    MlleBun and dyupa like this.
  3. LukaKotar

    LukaKotar

    Joined:
    Sep 25, 2011
    Posts:
    394
    Try something like this:
    Code (CSharp):
    1. Vector3 offsetPoint = Vector3.Lerp(hit.point, rayOrigin, 0.25f);
    The 'rayOrigin' is your starting position for the ray. Increasing 0.25f to something bigger will move it closer to the ray origin. (1 is at the origin, 0 is at hit.point)
     
    Last edited: Jun 23, 2016
    dyupa likes this.
  4. Gibbonuk

    Gibbonuk

    Joined:
    Dec 10, 2013
    Posts:
    175
    Thanks, tried both (just out of interest) and found both seem to do the trick. Thanks