Hi i have a method that change layers like down below. With layer change i change the color of object with shader.
private void SetLayerRecursively(GameObject obj, int newLayer)
{
if (null == obj)
{
return;
}
obj.layer = newLayer;
foreach (Transform child in obj.transform)
{
if (null == child)
{
continue;
}
SetLayerRecursively(child.gameObject, newLayer);
}
}
And i have a code that raycast controlling which object in the mouse position like down below.
private void RaycastHitCheck()
{
Ray CameraRay = PlayerCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
//Raycast Hit Pickup and Highlight Layers
if (Physics.Raycast(CameraRay, out RaycastHit HitInfo, PickupRange, LayerMask.GetMask("PickUp", "Highlight")))
{
CurrentObjectNoTake = HitInfo.rigidbody;
if (Input.GetKeyDown(KeyCode.E) && !isObjectInHands && CurrentObjectForPlace == null)
{
SetLayerRecursively(CurrentObjectNoTake.gameObject, pickupMask);
isObjectInHands = true;
}
if (Input.GetMouseButtonDown(0) && !isObjectInHands)
{
SetLayerRecursively(CurrentObjectNoTake.gameObject, pickupMask);
}
//Raycast Hit But Dont Press E
if (!isObjectInHands && CurrentObjectNoTake != null)
{
SetLayerRecursively(CurrentObjectNoTake.gameObject, highlightMask);
}
}
//Raycast Doesn't Hits Anything
else
{
if (CurrentObjectNoTake != null)
{
SetLayerRecursively(CurrentObjectNoTake.gameObject, pickupMask);
}
}
}
I deleted the unnecessary code so you can see it better thats why there is a lot of ifs.
The problem is
when raycast hit object outside pickup layer there is no problem but when raycast hit pickup to pickup object again both of them staying highlighted color
Like you see in the video