Search Unity

WorldToScreenPoint() inconsistent results

Discussion in 'Scripting' started by magwo, Jul 10, 2009.

  1. magwo

    magwo

    Joined:
    May 20, 2009
    Posts:
    402
    If I have a script doing WorldToScreenPoint() in a fairly static situation.. sometimes the result seems to vary, which causes GUI elements to flicker between two positions even though nothing is changing in the scene.

    What's causing this and how can it be avoided? I've considered some algorithmical approaches to filtering out the position flickering, but the change is quite unpredictable.
     
  2. magwo

    magwo

    Joined:
    May 20, 2009
    Posts:
    402
    Bumpedy.
     
  3. Digitalos

    Digitalos

    Joined:
    Jun 1, 2009
    Posts:
    112
    Can you post the script, what is this objects position too?
     
  4. magwo

    magwo

    Joined:
    May 20, 2009
    Posts:
    402
    Well, the camera is attached to a completely resting rigidbody. Maybe gravity and corresponding collision response is causing some very small changes in position or rotation of the rigid body.
     
  5. Digitalos

    Digitalos

    Joined:
    Jun 1, 2009
    Posts:
    112
    Hey magwo, can you post a link to the script? I have used this stuff a fair bit in our project and the results are pretty consistent so far. Here is a quick example of one area we use it and I can't see any visible flickering:

    bool mouseRayHit = Physics.Raycast( Camera.main.ScreenPointToRay( Input.mousePosition ), out hit, 1000, CrosshairLayerMask);

    We are then placing the cursor as appropriate. The reason I asked position is because if you are very far from the origin there could be some floating point inaccuracy when getting the world position?
     
  6. magwo

    magwo

    Joined:
    May 20, 2009
    Posts:
    402
    Well, it shouldn't matter if there's floating point accuracy issues, because the matrices and everything should be the same so the end result should be the same.

    I was unable to reproduce it in a simple project - I will try with rigid body and all that stuff, and post it.
     
  7. magwo

    magwo

    Joined:
    May 20, 2009
    Posts:
    402
    Edit: Alright, this happens when you attach a camera to a rigidbody, then set its transforms every frame aswell.