Search Unity

Raycast mouse-picking is flickering

Discussion in 'Scripting' started by kingIZZZY, Nov 29, 2009.

  1. kingIZZZY

    kingIZZZY

    Joined:
    Nov 26, 2009
    Posts:
    29
    <noob alert>

    Trying to implement a "Mouse over object / Mouse click on object" functionality and after much browsing it seems Physics.Raycast would be the way to go (correct me if i'm wrong). Here is some crap I coded in a "InputHandler.js" JavaScript file which I attached to a 'InputHandler' GameObject;


    Code (csharp):
    1. var lastUpdateWasAHit : boolean;
    2. var lastHit : RaycastHit;
    3.  
    4. function Update ()
    5. {
    6.     var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    7.     var hit : RaycastHit;
    8.     if(Physics.Raycast (ray, hit, Mathf.Infinity)  lastUpdateWasAHit != true)
    9.     {
    10.         // to represent that the picked object is 'selected', we add some gray
    11.         hit.collider.gameObject.renderer.material.color += Color.grey;
    12.         lastHit = hit;
    13.         lastUpdateWasAHit = true;
    14.     }
    15.     else // if raycast didnt hit anything
    16.     // we still gotta clean up our last hit (if any)
    17.     if(lastUpdateWasAHit)
    18.     {
    19.         // remove the gray that we added when it became 'selected'
    20.         lastHit.collider.gameObject.renderer.material.color -= Color.grey;
    21.         lastUpdateWasAHit = false;
    22.     }  
    23. }
    1. It works, but the objects that get selected flicker horribly between regular and greyed material when I mouse-over them. Why dont they stay stationary with the added gray?

    I even tried adding some code to only make it Raycast if there has been any mouse input. It did help for when the mouse was stationary, but when I do move the mouse it flickers again, even if i move the mouse while still remaining only over this object. Its as if the raycasts keep alternating between reporting that they hit the object and reporting that they didnt hit anything, even when the mouse is defiitely right over the object (and has been reported as a successful raycast hit only frames before!)

    2. Am I doing things right? Is there some other more correct way to go about doing this kinda thing? I'm implementing my own "Input Manager" kind of (instead of using OnMouseEnter etc) because I need to code tons of game states to control what input combinations should have what kind of effect in different game states... wish Unity had something built in to facilitate this.
     
  2. KaelisAsur

    KaelisAsur

    Joined:
    Apr 9, 2009
    Posts:
    361
    Its a bug, and only happens in the editor. It will work as advertised in webplayer and standalone.
     
  3. kirsis

    kirsis

    Joined:
    Nov 19, 2009
    Posts:
    31
    edit: sorry, was in a rush, did't notice that you'ŗe specifically avoiding OnMouseX functions. Disregard my post :)

    For mouse "over object/click on object" type functionality, I suggest using

    OnMouseOver
    OnMouseExit
    OnMouseDown (or OnMouseClick? I forgot)
    etc. functions (see scripting reference)

    Just keep in mind that even though these functions don't require you to use raycasting directly, the game objects still can't be in the "ignore raycast" layer.

    Cheers
     
  4. kingIZZZY

    kingIZZZY

    Joined:
    Nov 26, 2009
    Posts:
    29
    Thanks for the help guys.

    Turns out some of my logic in the code (see above use of 'lastUpdateWasAHit') was causing the alternating effect every frame...
     
  5. numz

    numz

    Joined:
    Jan 26, 2010
    Posts:
    1
    Hi,
    your code was a good help to understanding raycast.

    I attempt to remove the flickering effect too.
    Can you share the way you manage that ?

    Thank you.
     
  6. VRMan

    VRMan

    Joined:
    May 21, 2009
    Posts:
    1
    Yeah I'm sure someone else told you by now but it was bcuz you were Adding the grey color to that rather than just setting the color solid like... (it would could up adding whatever value grey was, incrementing by that continually...=)_

    Mouse picking very useful, use it a lot, and I do endless raycasting, entire vehicle phy is most often approximated using raycasting. (but for this stuff you'd use the OnMouseOver stuff the other guy mentioned, inside the script for the object you want pickable...) Good stuff.... good luck.

    --==[/ www.vrman3d.com \]==--