- Home /
OnMouseExit Problems vs. RaycastAll
Hey guys,
thanks for being such a great community.
I have this grid of GameObjects and i want to make not only the one transparent that the mouse rests on, but also those behind it - so i am sending a RaycastAll to lower the alpha of those hit.
RaycastHit[] hitall = Physics.RaycastAll (ray, 1000, ObjectLayer);
for (int i = 0; i < hitall.Length; i++)
{
RaycastHit hit = hitall [i];
if (hit.collider.transform.childCount > 0)
{
Summary: set alpha from children of hit.collider.GameObject to 0.3f
}
}
To reset the alpha back to normal when the mouse is not over the GOs anymore, i am using OnMouseExit on the Cube class.
public void OnMouseExit()
{
if (this.transform.gameObject.layer != 8 && this.transform.childCount > 0)
{
Summary: Set all childrens' transparency back to 0.7f
}
}
This seems to almost work, but weirdly enough when i move the mouse "inward" from the outside of the grid to the inside, the alpha of the GOs is not changed back except for the last one the cursor hit. When i move the cursor "outward", it works just like i intended.
Can anybody point out to me what the problem is here?
Thanks a lot in advance!
All the best.
I'm a bit unclear - RaycastAll method always works, but the On$$anonymous$$ouseExit sometimes fails? Can you deduce anything further about the scenario which might account for these see$$anonymous$$gly-random failures?
It totally worked. If you post it as a proper answer, i can give you the green tick mark. Thanks a lot for your help!
Answer by JinxM_ · Nov 19, 2014 at 04:18 PM
Alternative guess here.. You're using RaycastAll to make things transparent, which goes through colliders.
Conversely, you're using OnMouseExit to turn things opaque, which might not go through colliders.
Due to the angle of the camera, when you move the mouse to the lower left, you reach a point where tiles overlap. OnMouseExit stops at the new blocking tile and turns the tile underneath opaque, but RaycastAll goes through and turns it transparent again.
When you move the mouse towards the upper right, OnMouseExit turns the tile opaque and RaycastAll also misses at the same time, resulting in the correct behaviour.
I'm not certain that's how OnMouseExit works, but it seems the likeliest explanation. AlwaysSunny has a good solution. An alternative is to save a list of the transparent tiles and turn opaque the ones that no longer appear in your next RaycastAll. (I would use the LINQ operator "Except" for this.)
/facepalm/ - I have no clue why I didn't make that connection. +1 to you for making me feel silly while directly answering the OP's question. :)
Thank you very much, that totally made sense.
For anybody reading this in the future - just go with AlwaysSunny's suggestion below. It's easy and works like a charm. Thanks a lot to both of you, i wish i could checkmark both answers.
Answer by AlwaysSunny · Nov 12, 2014 at 11:35 PM
When I approach situations like this, where some temporary activity should change the state of an object, I generally try to avoid using any OnExit type of event. This is largely because certain conditions will bypass the OnExit family of calls, such as quitting or pausing and resuming the game under altered input conditions. Or, you run into weirdness like you're currently experiencing. They just don't seem ironclad, and I like ironclad, even at the expense of extra work and CPU workload.
If the situation permits - and it sounds like yours does - I'd let my RaycastAll method trigger a method on your objects which places them in a FadeOut state. Somewhere in LateUpdate, if FadeOut wasn't triggered this frame, switch to FadeIn. It feels a little sophomoric to do this, since you're creating extra work for the CPU, but if your events don't work under the most basic conditions and you can't determine why, this presents a workable alternative.
Make sense? Best of luck,
Your answer
Follow this Question
Related Questions
how to check if 1 sided plane is facing the camera 1 Answer
Drawing Sector around GameObject in GameView: GUITexture or 3D Object 1 Answer
Make the player shoot towards a mouse click 2d Platformer 1 Answer
Raycast and box collider not working properly 2 Answers
Clicking multiple objects with same tag, how to "forget" the one clicked previously... 0 Answers