Hiho.
For our GUI we’re using 3D-objects (since the GUI stuff is not flexible enough for us), that are attached to the Camera. However, when the camera is moving (and therefore, the button, as a child of the camera, is moving along with it), Physics.Raycast no longer hits the button reliably (or at all), and seems to refer to object positions of (at least) the previous frame.
So although the Button is a direct child of the Camera, and the Physics.Raycast is fed the frame-current position of that camera, it fails to see the button at its current position - it appears the collider used to do the intersect testing still refers to an outdated position of that child.
When doing the camera movement in Update() and the Raycast in the button script also in Update(), the results are pretty weird. This might be understandable, since it is undefined which Update() function is processed first.
However, even when doing the Raycast in LateUpdate(), it still doesn’t work, as you can reproduce by yourself:
In the testcase, there is a button attached to a camera, and a cube somewhere farther away. A script attached to camera and cube will translate them along -X, so when pressing Play you won’t see anything move except the point light:
- the cube appears stationary because it moves at the same speed as the camera
- the button appears stationary because it is attached to the camera
- the plane appears stationary because it is of constant color and very large (it actually is stationary, but not relative to the camera)
The Raycasting script in LateUpdate() is attached to the Cube and the Button. They both test the whole scene.
If you now click at A, both Raycasts correctly give “Button” as a result.
Clicking at B will give “Button” most of the time, but in about 1/10 cases, both will return “Cube”! (without moving the mouse!)
Clicking around C (a few pixels to the left/right make a huge difference here), will either always return the Plane (although both Cube and Button are in front), a few pixels to the right both will return Plane and Button 50/50, and a few pixels more to the right both will return Button and sometimes Plane - never Cube.
Finally, clicking around D will always return Plane, never Button.
Is this a known bug?
Is there a workaround? Because at the moment, we cannot interact with our GUI as long as the camera is animated/in motion (which of course happens frequently) :-/
BTW, Unity 3b5 has the same bug.
(for reference this is Windoze Vista, Pro version, and I get framerates of about 200fps)
Thank you for any help on this.
EDIT: Maybe the real question is, when are the data structures used for Physics.Raycast updated? Or is there a command to force an update? Is Physics.Raycast only allowed in FixedUpdate? (which would be bad, since objects can be moved around all the time)
For example, we had some real trouble when we tried to adapt some of the uniTUIO scripts to our needs - we noticed the Unity Editor will simply crash when Physics.Raycast is called outside the Update() functions (i.e., whenever an asynchronous TUIO touch event was received). Is the answer to that “Yes, that is to be expected”, or is it “Oops, that shouldn’t happen”?
EDIT2: Oh, I just realized the testcase I attached was for Unity3, and the scenes apparently are not backwards compatible. So I also attached a 2.6 testcase, which also extends the original testcase by a cube with a Unity animation. Switch between ScriptedAnim_Update, ScriptedAnim_FixedUpdate, and the Animation with “AnimatedPhysics” on/off for both cubes and the camera, for the different behaviours. Note: everytime you see a jerky cube, that’s to be expected, since either all objects need to use FixedUpdate, or all need to use Update.
365548–12690–$raycastbugtestcase_200.unitypackage (27.8 KB)

365548–12942–$raycastbugtestcase_unity26_144.unitypackage (9.35 KB)