Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Raycast Sciprt issue

Discussion in 'Scripting' started by Acazied, May 5, 2016.

  1. Acazied

    Acazied

    Joined:
    Mar 2, 2016
    Posts:
    47
    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 )
    Code (CSharp):
    1.      
    2. if(Default_CrossHairBool)
    3.         {
    4.             Default_CrossHair.gameObject.SetActive(true);
    5.             TreeCrosshairbool = false;
    6.             WoodCrosshairBool = false;
    7.         }
    8.       else
    9.         {
    10.             Default_CrossHair.gameObject.SetActive(false);
    11.         }
    12.         if (TreeCrosshairbool )
    13.         {
    14.      
    15.             TreeCrosshair.gameObject.SetActive(true);
    16.  
    17.             Default_CrossHairBool = false;
    18.             WoodCrosshairBool = false;
    19.         }
    20.         else
    21.         {
    22.             TreeCrosshair.gameObject.SetActive(false);
    23.         }
    24.         if(WoodCrosshairBool)
    25.         {
    26.             WoodCrosshair.gameObject.SetActive(true);
    27.             Default_CrossHairBool = false;
    28.            TreeCrosshairbool = false;
    29.  
    30.         }
    31.         else
    32.         {
    33.             WoodCrosshair.gameObject.SetActive(false);
    34.         }
    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 :/
    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 :/
    Code (CSharp):
    1.  
    2. RaycastHit hit;
    3.         Vector3 rayOrigin = fpscam.ViewportToWorldPoint(new Vector3(.5f, .5f, 0));
    4.         if (Physics.Raycast(rayOrigin, fpscam.transform.forward, out hit, RayRange))
    5.         {
    6. //check if the  it s a tree
    7.             if (hit.collider.CompareTag("Tree"))
    8.             {
    9. //change Crosshair
    10.                 CrossHairMang.TreeCrosshairbool = true;
    11.                 CrossHairMang.Default_CrossHairBool = false;
    12.                 CrossHairMang.WoodCrosshairBool = false;
    13.                 Debug.Log("whaz0");
    14.  
    15.                 if (Input.GetButtonDown("Fire1") && Time.time > NextTimeHit)
    16.                 {
    17. // Hit the tree
    18.                     NextTimeHit = Time.time + hitRate;
    19.                     print(NextTimeHit);
    20.                     treeState = hit.collider.gameObject.GetComponent<TreeState>();
    21.                     treeState.THealth -= TreeDamage;
    22.                     print("u hit a tree");
    23.                 }
    24.             }
    25.             //Still in raycast If Statment
    26.             if (hit.distance > RayRange)
    27.                 {
    28.                // now if Out of range change the crosshair to default one
    29.                     CrossHairMang.Default_CrossHairBool = true;
    30.                     CrossHairMang.TreeCrosshairbool = false;
    31.                     CrossHairMang.WoodCrosshairBool = false;
    32.                 }
    33.             }
     
  2. Acazied

    Acazied

    Joined:
    Mar 2, 2016
    Posts:
    47
    bump maybe
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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

    2)
    Code (csharp):
    1.  
    2. if(Physics.Raycast(rayOrigin, fpscam.transform.forward, out hit, RayRange))
    3. //...
    4. if(hit.distance> RayRange)
    5.  
    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.
     
  4. Acazied

    Acazied

    Joined:
    Mar 2, 2016
    Posts:
    47
    2) 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
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. if(hit==null) { /...
    3.  
     
  6. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,886
    Physics.Raycast returns true or false, so you just check the false part to see when it's not hitting.

    Code (csharp):
    1. if (Physics.Raycast(...))
    2. {
    3.     // Hitting something
    4.     CrossHairMang.Default_CrossHairBool = false;
    5. }
    6. else
    7. {
    8.     // Not hitting anything
    9.     CrossHairMang.Default_CrossHairBool = true;
    10. }
    You should use enums for the crosshair as mentioned above.
     
  7. MaximilianPs

    MaximilianPs

    Joined:
    Nov 7, 2011
    Posts:
    321
    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) ?
    Code (CSharp):
    1. Physics.OverlapSphere(myPosition.transform.position, sphereRadius);
    'cause I guess I could use it on each #frames to check for enemies (for example)
     
  8. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,886
    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.
     
    MaximilianPs likes this.