Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to increase Raycast length??

Discussion in 'Scripting' started by GrandAlchemist, Apr 13, 2015.

  1. GrandAlchemist

    GrandAlchemist

    Joined:
    May 21, 2014
    Posts:
    10
    Just wondering if someone could give me some insight on why I cannot increase my raycast ray.
    It seems as if it just defaults to 1 unit length.

    In float maxDistance parameter, it is ignoring my rayDistance float... What am I missing??

    Code (csharp):
    1.  
    2.  
    3. private float rayDistance = 15;
    4.  
    5.     void Update ()
    6. {
    7.         RaycastHit hit;
    8.         Ray aimRay = new Ray(transform.position, transform.forward);
    9.         if (Physics.Raycast(aimRay, out hit, rayDistance))
    10.         {
    11.             if (hit.collider.tag == "Player")
    12.             {
    13.                 Debug.Log("Hit Player");
    14.             }
    15.         }
    16. }
    17.  
     
    Last edited: Apr 13, 2015
  2. relic1882

    relic1882

    Joined:
    Mar 11, 2015
    Posts:
    45
    Are you positive that it is only going 1 unit length? Try making rayDistance something extravagant like 1000 or something. Maybe use a Debug.DrawLine to get an exact idea on how far it's actually going. Sometimes things are not scaled as perfect as you'd like depending on the setup you have going for you. Just my two cents. :)
     
  3. GrandAlchemist

    GrandAlchemist

    Joined:
    May 21, 2014
    Posts:
    10
    Thanks for the input! I did end up figuring it out. I had some trigger boxes as children to the player, so the ray was hitting those, not reaching the player therefor not detecting the collision.