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

Physics.Raycast maxDistance only working with int?

Discussion in 'Scripting' started by Shushustorm, Sep 23, 2017.

  1. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Hey everyone!

    When taking a look at the documentation (I am using Unity 5.4, so I look at https://docs.unity3d.com/540/Documentation/ScriptReference/Physics.Raycast.html ), Physics.Raycast seems to use a float for maxDistance.

    But when I try to limit the distance of a raycast, it doesn't seem to work using floats.

    Code (CSharp):
    1. if (Physics.Raycast(playerHead.position,playerHead.forward,2)) {
    2.  
    3. }
    works as intended.
    However, this doesn't:

    Code (CSharp):
    1. float rcDistance = 2.0f;
    2. if (Physics.Raycast(playerHead.position,playerHead.forward,rcDistance)) {
    3.  
    4. }
    Is this to be expected?

    Best wishes,
    Shu
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    What does it do? If it takes a float, it's converting the int to a float because there is no data loss, so it should be exactly the same. There's an example using 100.0f in the section you referenced.
     
    Last edited: Sep 23, 2017
    Shushustorm likes this.
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Raycast only uses floats for maxDistance; it can't use an int. If you supply an int, it's converted to a float instead. So yes, it works with floats, and that's all it works with.

    --Eric
     
    Shushustorm likes this.
  4. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    @fire7side
    @Eric5h5

    Thanks for your answers!
    Well, that's weird. When I use an int for maxDistance, everything seems to be working just as intended.

    However, when I use a float for maxDistance (directly), maxDistance will be very small, maybe about 0.1f (no matter what I set the value to).

    The code inside the if statements is reached like this:

    Code (CSharp):
    1. if (Physics.Raycast(playerHead.position,playerHead.forward,10)) {
    2.     // actually works at distance of 10 or lower
    3. }
    Code (CSharp):
    1. float rcDistance = 1000.0f;
    2. if (Physics.Raycast(playerHead.position,playerHead.forward,rcDistance)) {
    3.     // code only works at distance of about 0.1f or lower
    4. }
     
  5. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819

    I just tried it with basically your code and there was no difference. I do have version 5.4 or something. I should upgrade. I highly doubt there is a problem with it especially because it requires a float. I would guess your value is somehow being zeroed out. I'm not sure if that is your exact code or what. It certainly looks all right. That's what I did and it worked fine. I have trouble believing that something as common as a raycast wouldn't show up especially when the requirement is a float. Did you try entering the float in the raycast rather than using a variable? I'm pretty sure you would get exactly the same result. I did it directly, and with a variable with the same result.
     
    Shushustorm likes this.
  6. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    @fire7side

    Thanks for testing!
    I just modified the code back to using maxDistance, to test it another time, just to make sure.
    (Before, I used Vector3.Distance() to get the distance as a workaround.)
    I don't know what went wrong when testing last time, but this time, everything works as expected using a float.
    Maybe I updated the script at runtime, which maybe isn't the best idea.

    Something like this works now:
    Code (CSharp):
    1. RaycastHit rcHit;
    2. float rcDistance = 10.0f;
    3. void Update () {
    4.  
    5.     if (Input.GetKeyDown(KeyCode.I)) {
    6.         Debug.Log("KEY DOWN: I");
    7.         if (Physics.Raycast(
    8.             playerHead.position,
    9.             playerHead.forward,
    10.             out rcHit,
    11.             rcDistance
    12.         )) {
    13.             Debug.Log("RC HIT @ 10.0f");
    14.             string rcHitTag = rcHit.transform.gameObject.tag;
    15.             string rcHitType = rcHitTag.Substring(0,2);
    16.             if (rcHitType == "c_") {
    17.                 Debug.Log("COLLECTING ITEM");
    18.                 // collecting item
    19.             }
    20.             // some other object rcHitTypes
    21.         }
    22.     }
    23.  
    24. }