- Home /
Raycast2D always "remembers" it's last hit
Hello.
I have multiple 2D sprites in my scene and I got GameObject that just tracks mouse and checks if it hovers the other GameObjects with colliders:
void Update () {
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
if (hit.collider != null) {
Debug.Log("YES");
} else {
Debug.Log("NO");
}
}
So when I first launch my scene it shows NO. But after it hits one of theese objects its says YES and not change even if my mouse does not goes over another objects anymore. What I have missed? I know that there is OnMouseEnter, OnMouseLeave functions, but I do not want to write it to every script.
ScreenToWorldPoint requires a Vector3. Z is the depth of the point.
Are you sure that you want to Raycast with the Vector2.zero? That will basically not raycast in any direction, and I believe it will only detect if the screen point is within an object.
I have found it from forums. What do you suggest?
All I want to do is write YES when mouse over some object with 2D collider and NO when it is not. I can't use On$$anonymous$$ouseEnter like functions.
O$$anonymous$$ I if Vector3.Z is depth then I changed code like that:
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = -10f;
Vector3 pos = Camera.main.ScreenToWorldPoint(mousePosition);
RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
Still the same.
maybe you should consider trying viewportToWorldPoint ins$$anonymous$$d
Answer by SuperMasterBlasterLaser · Jun 17, 2014 at 04:30 PM
I have found solution. It Actually shows me YES and NO in situation that I want. Editor groups messages from Debug.Log that are same in one row and just shows its numbers. Then it sorts it alphabeically and when maximize on play is turned on it always shows me the LAST message.
This made me to think that Debug.Log always shows me YES. How I found it out? I just made counter and attached it like that:
if (hit.collider != null) {
Debug.Log("YES " + counter);
} else {
Debug.Log("NO " + counter);
}
When I started the game now It showed me what I want. So, I am sorry for wasting your time, anyway thank you for attention.
@Super$$anonymous$$asterBlasterLaser oh I see. Good thinking.
Answer by SquigglyFrog · Jun 17, 2014 at 05:41 PM
Another way to do this is instead of using a debug.log, use a print statement.. it doesnt seem to group those.. found this issue with grouping myself.. print is just easier than using a counter variable..
or you could just make sure collapse is not enabled in the console or look how many times it has been logged on the right of the notification
I've never noticed the grouping. I always put lots of information into my Debug.Log's. Between the specific call, the object calling, and the timestamp, there's never anything to group...
Your answer
Follow this Question
Related Questions
Raycasting in 2D is not working 0 Answers
Clamp a mouse around several 2d objects 1 Answer
Raycast for a 2D objects 0 Answers