Raycast only hits collider the first time

Hi,

I’m experiencing a bizarre problem with raycasts.

Basically, I’m using ScreenPointToRay and Physics.Raycast to see if the user has clicked on an object (with a single BoxCollider, usually). If so, any further mouse motions drag the object around.

However, the second time you click on the object (e.g., to move it around again), the raycast doesn’t register a hit. However, if you go to the inspector and edit any value of the collider (e.g., change size.x from 28 to 28.1), the next time you click on the object, the raycast will again register a hit. And, after that, not register any hits after that.

I’ve tried adding kinematic non-kinematic rigidbodies and it doesn’t help.

Weirdly, if you drag it within a very small distance of its origin, you can continue to drag it over and over.

Any suggestions? Is there anything I’m missing here?

Thanks!

Unless you’re doing something really, really odd, this does sound like a Unity bug.
If you try dragging the second time from the original position, does the raycast catch it there? It could be that the collider isn’t being properly moved, for whatever reason.

If you can’t find anything you’re doing wrong, I’d file a bug report with a reproducible test case to UT.

Ok, did a little more testing - basically, I found that the collider.bounds were not being updated when I moved the object from place to place. So, if I moved it a very short distance, the collider would still respond the next time because it was “close enough”. You would think, then, that if I moved the object far away, I could still click on its original (now empty) location to trigger the raycast (since the collider’s bounds were the same), but confusingly that didn’t work either.

If I moved the object in the editor and then checked the bounds in-game, they would be updated. If I moved the object in-game and then modified the collider values in the inspector, the bounds would be updated.

So, this all seems very buggish to me.

Fortunately, I thought of one more thing - maybe I could trick the collider into resetting! I tried adding and removing a rigidbody after every drag, and voila - total object-dragging happiness. Thank goodness for hackery. Where would I be without it?

Seriously though, Unity folks, is this a bug? Or am I just doing things all wrong? :slight_smile:

Oh. Ohhhhhhhhh. Yes. “All wrong” is right.

Note to interweb denizens: do not set Time.timeScale = 0 and expect your colliders to update…

D’OH!

Odd, I get the same issue, but with the timescale at 1. The collider bounds don’t update when moving the object in the editor. Renderer bounds work fine though, but that isn’t always what I’m wanting to work with…

Edit: It seems to work fine in the Unity 3 beta, can’t wait for the final release on that one :slight_smile:

I’m also seeing this as a bug. Are there Unity employees who check this forum? Can you comment on this?