Set Object as active if Raycast hits nothing
I have a Grappling Hook mechanic which can be performed when aiming (holding Fire2). I have a regular white crosshairs for when the player isn't aiming. A green one for when aiming and the raycast hits an object with the correct Tag. I also have a red crosshairs for when the player is aiming and hits either something with the wrong tag or nothing at all.
The problem is that when I start aiming while looking at something with the wrong tag (not even mentioning looking at the sky yet), the crosshairs doesn't change to red, if I'm looking at the right tag and start aiming, the crosshairs changes to green and changes back to white when releasing Fire2 or using the Hook, which is exactly the behavior I want.
But if aiming to the right tag, then I rotate towards the wrong tag it won't change to red crosshairs, if I release Fire2 with the green crosshairs looking at the wrong tag (first it should've changed to red), it stays stuck and doesn't change back to white until I look at something with the right tag, then hold and release Fire2
I have set for debugging purposes a raycastHitObject which retrieves the object hit by the raycast if it also contains the right tag, else the object stays null. I also set a bool foundTarget which turns to true if the raycast hits the right tag, if it doesn't hit anything or hits the wrong tag, the bool will stay false.
Both of these work as expected and update in real time in play mode. I've tried checking if the bool is true/false and if object !/null (also tried using a placeholder instead of null and replace with the target if hit by raycast and checking if object != placeholder also with no luck) to trigger my desired behavior with no luck.
public class GrapplingHook : MonoBehaviour
{
#region Variables
//GameObjects
public GameObject Hook;
public GameObject redCrosshairs, targetFoundCrosshairs, regularCrosshairs;
//Dynamic GameObjects (changed on the fly)
public GameObject raycastHitObject;
//Misc References
public RaycastHit hit;
public Camera raycastCamera;
//Bools
[SerializeField] bool isAiming;
//debug bools
public bool targetFound;
#endregion
void Update()
{
Physics.Raycast(raycastCamera.transform.position, raycastCamera.transform.forward, out hit, maxDistance);
Debug.DrawLine(raycastCamera.transform.position, hit.point, Color.red);
if (hit.transform == null || hit.transform.tag != "Hookable")
{
raycastHitObject = null;
targetFound = false;
return;
}
if (hit.transform.name != null && hit.transform.tag == "Hookable")
{
raycastHitObject = hit.transform.gameObject;
targetFound = true;
}
if (!isAiming)
{
redCrosshairs.SetActive(false);
targetFoundCrosshairs.SetActive(false);
regularCrosshairs.SetActive(true);
}
if (isAiming && targetFound)
{
regularCrosshairs.SetActive(false);
redCrosshairs.SetActive(false);
targetFoundCrosshairs.SetActive(true);
}
if (isAiming && !targetFound)
{
regularCrosshairs.SetActive(false);
targetFoundCrosshairs.SetActive(false);
redCrosshairs.SetActive(true);
}
}
}