Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Raycast is not getting the right distance.

Discussion in 'Physics' started by OldManTies, Aug 12, 2016.

  1. OldManTies

    OldManTies

    Joined:
    Aug 12, 2016
    Posts:
    5
    In my project I'm using a laser which is just a linerenderer with a raycast, it is supposed to adjust the 2nd position of the LineRenderer component with the raycast distance (hit.distance), but the distance is always way too short. When the Raycast says the disance is 9 it is actualy 13 (and 3 is actualy 4). So its not that it is placed too far back because the offset is relative....

    This is my code.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Laser : MonoBehaviour {
    5.  
    6.     private LineRenderer lr;
    7.     // Use this for initialization
    8.     void Start () {
    9.         lr = GetComponent<LineRenderer>();
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.         RaycastHit hit;
    15.  
    16.         if (Physics.Raycast(transform.position, transform.forward, out hit))
    17.         {
    18.             if (hit.collider)
    19.             {
    20.                 lr.SetPosition(0, new Vector3(0, 0, 0));
    21.                 lr.SetPosition(1, new Vector3(0, 0, hit.distance));
    22.             }
    23.         }
    24.         else
    25.         {
    26.             lr.SetPosition(1, new Vector3(0, 0, 1000));
    27.         }
    28.     }
    29. }
    30.  
    Could anyone please tell me what is wrong with my code?

    btw.
    I have tried it using vector3
    Code (CSharp):
    1. // instead of hit.disance something along the lines of:
    2. Vector3 laserDistance = hit.collider.transform.position - transform.position;
     
  2. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Is transform.position always 0,0,0? Because currently that is your relative location to the drawn distance. I was expecting:
    Code (CSharp):
    1.  
    2. lr.SetPosition(0, transform.position);
    3. lr.SetPosition(1, transform.position + new Vector3(0, 0, hit.distance));
    Also on hit you can do Debug.log(hit.collider.name);
     
  3. OldManTies

    OldManTies

    Joined:
    Aug 12, 2016
    Posts:
    5
    Ive already got the hit.collider.name right. It's just not gettting the right distance to the object..
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    hit.collider.transform.position- transform.position wont give you distance. I think it gives you direction (or something... im tired and feeling vague)

    easy distance: Vector3.Distance(hit.collider.transform.position, transform.position)

    I think (hit.collider.transform.position- transform.position).sqrMagnitude is also distance, although ive never used that method, despite hearing its fastest of the lot
     
  5. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    sqrMagnitude is mainly useful/faster for comparing distances, such as if vector1.sqrMagnitude > vector2.sqrMagnitude. sqrMagnitude doesnt give you the actual useful distance, it gives you the distance squared. This means if you know the distance you want to compare sqrMagnitude with, you can do it like vector1.sqrMagnitude == distance * distance.

    Also, dont use the collider transform for the distance, use the actual hit.point.

    (vector1 - vector2).magnitude gives the same result as the Vector3.Distance method, though I am not sure if one is more optimized for the square root operation.

    To OP, I am unsure as to what you mean by "When the Raycast says the disance is 9 it is actualy 13". How were you able to tell it was actually 13? You debug.log the hit.distance and it returned 9 while in the scene view you checked yourself to see it was 13?
    What happens if you do this
    Code (CSharp):
    1. if (hit.collider)
    2. {
    3.     lr.SetPosition(0, transform.position);
    4.     lr.SetPosition(1, hit.point);
    5.  
    6.     Debug.Log(hit.distance + " " + Vector3.Distance(transform.position, hit.point));
    7. }
    If the line doesnt seem its hitting the right object or the two distances dont match, then there might be an issue with how the cast is detecting your collider, possible causes are too large objects or weirdly scaled objects.