- Home /
Checking if object stopped being hit by Raycast while overlapping another object of the same type.
Hi everybody.
I'm having issues while using Raycast as a method of selecting objects ingame. On my camera, I have a script that manages the mouse actions, and inside the update I have this extract that manages the highlighting of objects ("highlightObject" and "clickBox" as the targets):
RaycastHit hitA;
if (!EventSystem.current.IsPointerOverGameObject()){
if (Physics.Raycast (ray, out hitA, 1000, layerMaskA)) {
layerID = hitA.transform.gameObject.layer;
if (layerID == 12 || layerID == 15 || layerID == 16)
{
HighlightOff(1);
highlightObject = hitA.transform.GetComponent<Highlighting>();
highlightObject.SetHighlightOn();
}
if (layerID == 10)
{
HighlightOff(0);
clickBox = hitA.transform.GetComponent<ClickBoxID>();
clickBox.SetHighlightOn();
}
}
else
{
HighlightOff(2);
}
}
else
{
HighlightOff(2);
}
The EventSystem thing keeps the Raycast go through UI elements, and the layers are set like that, because the objects make different actions based on the layer itself.
HighlightOff turn off the highlight depending of the action:
void HighlightOff (int type)
{
if (type == 0)
{
if (highlightObject != null)
{
highlightObject.SetHighlightOff();
highlightObject = null;
}
}
if (type == 1)
{
if (clickBox != null)
{
clickBox.SetHighlightOff();
clickBox = null;
}
}
if (type == 2)
{
if (highlightObject != null)
{
highlightObject.SetHighlightOff();
highlightObject = null;
}
if (clickBox != null)
{
clickBox.SetHighlightOff();
clickBox = null;
}
}
}
Everything almost works well, except when I try to select between objects with the same highlighting script, overlapping themselves:
The purple and red object have the same script, and when I, for example: Put the mouse over the purple one and go into the red one without exit the first one, the purple object keeps being highlighted without being checked. This problem occurs with objects of the same type and I want to solve it please.
Your answer
Follow this Question
Related Questions
Raycasting ignoring small sized object 1 Answer
Mouse raycast not working/not seeing line 0 Answers
[SOLVED] Raycast for custom button JS 1 Answer
Mouse Over Questions 1 Answer
Upper limits of Raycasting; Physics engine "gives up" 0 Answers