- Home /
Raycasthit info only working the first time the object is hit
Greetings, oh wise minds of the Internet.
I've run into an annoying issue while trying to use Physics.Raycast to identify the object hit. I use the following code to check objects on two layers (9 and 10) using the touchscreen (through the Android Remote) and it works, but only the first time I hit the object. Each subsequent hit on that object will do absolutely nothing...I can touch another object on those layers and it will update the Debug.Log message, but then that will only work once, too.
Here is the code:
var layerMask : LayerMask;
function Start () {
// Set the layer mask to "Floor" and "Radar"
layerMask.value = (1 << 9) | (1 << 10);
}
function Update () {
for (var touch : Touch in Input.touches) {
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Vector3(touch.position.x, touch.position.y,0));
Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
if (Physics.Raycast (ray, hit, Mathf.Infinity, layerMask)) {
Debug.Log(hit.collider.gameObject.name + " ("+hit.collider.tag+") ");
}
}
}
The code seems clean to me...any ideas why this only works the first time I "touch" an object, and not afterwards?
Thanks in advance!
EDIT: after some more testing, it seems the problem is in Debug.Log (I printed the info on-screen using GUI commands and it works...)
This kind of solves my problem, although it's strange that Debug.Log doesn't seem to work in this instance...)
A little off topic, but what does the line layer$$anonymous$$ask.value = (1 << 9) | (1 << 10); mean? I'm having trouble finding documentation on that notation.
It's the Bit Shift operator. There is a pretty good explanation here:
http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html
By the way, I did try without using a layer mask, and the result is the same: the object is identified as hit only the first time, afterwards nothing happens.
It's a feature of the Debug screen that it only shows the same message once. Clicking off Collapse will show all.
BTW I believe in the raycast, it should technically be layermask.value
. Your code doesn't throw any exceptions though so I'm a little stumped.. I glanced through the script reference and I didn't see any overloaded method that took a layermask or an implicit cast from layermask to int.
I'd be happy to hear what anyone can tell me about this. (I actually didn't know this struct existed until yesterday so I'm happy to learn more about it.)
Answer by Dreamer · Jun 07, 2011 at 02:08 AM
Uncheck "Collapse" button in your Console.
Yep, that´s the reason only the first Debug.Log is showing up.
@Dreamer, you just learned about that!
http://answers.unity3d.com/questions/126592/my-function-can-not-update.html
Your answer
Follow this Question
Related Questions
C# change an object tag wit raycasthit. 2 Answers
Using Layer Filter Raycast 0 Answers
Need raycast to target child, not parent. 0 Answers
Ignore one layermask question 1 Answer
Layer Mask Detection 2 Answers