Search Unity

BoxCast stops working after a while

Discussion in 'Scripting' started by rogerdv, Jun 27, 2019.

  1. rogerdv

    rogerdv

    Joined:
    Jul 16, 2012
    Posts:
    90
    Im implementing an AI system based on the ScriptableObjects AI tutorial. I have found a weird problem, after half a minute or something, the casts I use to check if the player character is "visible" stops working. The NPCs see the player a couple of times, but then they no longer see it. HEre is my code:

    Code (CSharp):
    1. [CreateAssetMenu (menuName="AI/Decisions/Look decision")]
    2.  
    3. public class LookDecision : Decision {
    4.  
    5.  
    6. `public override bool Decide(StateController controller) {`
    7.  
    8.    `bool TargetVisible = Look(controller);`
    9.  
    10.    `return TargetVisible;`
    11.  
    12. `}`
    13.  
    14.  
    15. `private bool Look(StateController controller) {`
    16.  
    17.    `RaycastHit[] results;`
    18.  
    19.    `results = Physics.BoxCastAll (controller.eyes.position, new Vector3 (4, 2, 4), controller.eyes.forward, controller.transform.rotation, 60.0f);`
    20.  
    21.  
    22.    `foreach (RaycastHit hit in results) {`
    23.  
    24.        `if (hit.collider.CompareTag("Player") && controller.GetFlag ("dead") == null) {`              
    25.  
    26. controller.gameObject.GetComponent<BaseCharacter>().target = hit.collider.gameObject;
    27.  
    28. return true;
    29.  
    30.        `}`
    31.  
    32.    `}//foreach`      
    33.  
    34.    `return false;`
    35.  
    36. `}`  
    37.  
    38. }
    I also tried replacing BoxCastAll by SphereCastAll, but the result is the same: after a while, the casts cant detect the player even when it is right in front of the NPC (or very close in any direction). Can somebody see something wrong here?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Try to remove filter
    Code (CSharp):
    1. if (hit.collider.CompareTag("Player") && controller.GetFlag ("dead") == null)
    and see if the cast hits any collider at all and what exactly does it hits.
     
  3. rogerdv

    rogerdv

    Joined:
    Jul 16, 2012
    Posts:
    90
    I tried that (I checked how many hits returned before the filter) and it is 0. For some reason, the cast works, and then simply stops detecting anything.
     
  4. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    I am getting problems with capsule cast too ,it just doesn't register nearby or touching object at all (I'm using unity 2019.1.3f1) Don't know what's the problem with it:mad:
     
  5. rogerdv

    rogerdv

    Joined:
    Jul 16, 2012
    Posts:
    90
    Forgot to mention Im using 2019.1.1. I have to try an old project in 2018.4 to see if it works.
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    This problem may be something I've encountered before, but it's been a long time so I might not remember the details. If you look at the documentation for SphereCast, though:
    https://docs.unity3d.com/ScriptReference/Physics.SphereCast.html
    In the notes it says: SphereCast will not detect colliders for which the sphere overlaps the collider.

    It does not say anything like this in the BoxCast documentation, but I imagine they both work in a similar way.

    Evidently, what that note means is that any colliders inside the sphere in it's starting position are excluded from the results. Assuming BoxCast works the same way, you might try doing an OverlapBox at the starting point, in addition to the BoxCast, and then loop through both sets of results.

    https://docs.unity3d.com/ScriptReference/Physics.OverlapBox.html
     
    Ruchir likes this.
  7. rogerdv

    rogerdv

    Joined:
    Jul 16, 2012
    Posts:
    90
    I think that BoxCastAll does detect objects in its starting position. At least, I noticed that the NPC object was detected, and I solved that by reducing the box in the Z axis to 0.5 or something like that.