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

weirdest raycast behavior ever

Discussion in 'Scripting' started by kattkieru, Apr 6, 2010.

  1. kattkieru

    kattkieru

    Joined:
    Jan 2, 2006
    Posts:
    130
    Hey folks,

    I've *never* had raycast issues before now, but check this out. Here's the code:

    Code (csharp):
    1. var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    2. var hit : RaycastHit;
    3. var isHit:boolean;
    4.    
    5. // I know I don't have to do this every frame but I like to see the
    6. // cast rays in the debug viewport
    7. isHit = Physics.Raycast (ray, hit, 150);
    8.  
    The result?
    http://vimeo.com/10707501
    (You should full-screen it to see the mouse and see the location GUI label.)

    I get collisions properly in three quadrants of my game board. The fourth quadrant returns nothing for the Z value; in fact, the fact that it doesn't return anything from its ToString() function means the value in Z is either not a number or some sort of error, I wager. I thought at first that maybe, somehow, the ray was hitting its limit before that quadrant, so I increased the depth to the above 150, but then I realized the length of the ray can't be longer than 50 at most.

    Then I had a crazy idea: since my board and the collider were sitting negative in all three axes, I flipped them to the positive. Boom. It all works. So I have a workaround. But I'd love to know if anyone else has experienced this.