Raycast Sciprt issue

i have 2 question
first about Bools
i didnt figure out how to make a bool that when activated it desactivats other active bools (this is for multiple crosshairs )

if(Default_CrossHairBool)
        {
            Default_CrossHair.gameObject.SetActive(true);
            TreeCrosshairbool = false;
            WoodCrosshairBool = false;
        }
      else
        {
            Default_CrossHair.gameObject.SetActive(false);
        }
        if (TreeCrosshairbool )
        {
      
            TreeCrosshair.gameObject.SetActive(true);

            Default_CrossHairBool = false;
            WoodCrosshairBool = false;
        }
        else
        {
            TreeCrosshair.gameObject.SetActive(false);
        }
        if(WoodCrosshairBool)
        {
            WoodCrosshair.gameObject.SetActive(true);
            Default_CrossHairBool = false;
           TreeCrosshairbool = false;

        }
        else
        {
            WoodCrosshair.gameObject.SetActive(false);
        }

firstly its not doing its job … i know what s wrong but i cant solve it and i dont like this script it s too long and due to my low Scirpting knowledge i cant make it better :confused:
second Question
is
Second question is i have a raycast when i get closer to something i can change the crosshair but when im out of range the crosshair doesnt change to default one :confused:

RaycastHit hit;
        Vector3 rayOrigin = fpscam.ViewportToWorldPoint(new Vector3(.5f, .5f, 0));
        if (Physics.Raycast(rayOrigin, fpscam.transform.forward, out hit, RayRange))
        {
//check if the  it s a tree 
            if (hit.collider.CompareTag("Tree"))
            {
//change Crosshair 
                CrossHairMang.TreeCrosshairbool = true;
                CrossHairMang.Default_CrossHairBool = false;
                CrossHairMang.WoodCrosshairBool = false;
                Debug.Log("whaz0");

                if (Input.GetButtonDown("Fire1") && Time.time > NextTimeHit)
                {
// Hit the tree
                    NextTimeHit = Time.time + hitRate;
                    print(NextTimeHit);
                    treeState = hit.collider.gameObject.GetComponent<TreeState>();
                    treeState.THealth -= TreeDamage;
                    print("u hit a tree");
                }
            }
            //Still in raycast If Statment
            if (hit.distance > RayRange)
                {
               // now if Out of range change the crosshair to default one 
                    CrossHairMang.Default_CrossHairBool = true;
                    CrossHairMang.TreeCrosshairbool = false;
                    CrossHairMang.WoodCrosshairBool = false;
                }
            }

bump maybe

  1. you might want to consider an enum instead of bools perhaps, if they’re all discrete states where only one can be true. You can then store the “currentState” and use a switch statement to do things based on the current enum value

if(Physics.Raycast(rayOrigin, fpscam.transform.forward, out hit, RayRange))
//...
if(hit.distance> RayRange)

if there isn’t anything within RayRange you wont be getting the “hit” instantiated at all, checking if it’s null would be the usual approach in that kind of situation.

  1. what i exactly want from the script is when i aim and in range of the tree i get a specific crosshair and when im not aiming or not at range it goes back the default cross if i say
    hit.distance == null that wont work and what i did is changed it to hit.collider == null but NOTHIng is working help me please
if(hit==null) { /...

Physics.Raycast returns true or false, so you just check the false part to see when it’s not hitting.

if (Physics.Raycast(...))
{
    // Hitting something
    CrossHairMang.Default_CrossHairBool = false;
}
else
{
    // Not hitting anything
    CrossHairMang.Default_CrossHairBool = true;
}

You should use enums for the crosshair as mentioned above.

Sorry If I gone a bit OT, but not so much at the end…

is this lighter (less heavy on CPU load) then a collider component (like a SphereCollider) ?

Physics.OverlapSphere(myPosition.transform.position, sphereRadius);

'cause I guess I could use it on each #frames to check for enemies (for example)

OverlapSphere only checks the bounding box, so it will be fast, but I wouldn’t use in Update unless you had to. You could also use a LayerMask so only important things were being hit instead of everything.

I normally use a Rigidbody SphereCollider with a script that stores a list of the enemies, but I don’t know the performance impact vs OverlapSphere.

1 Like