Search Unity

Inconsistent Physics.Raycast behavior. Hits sometimes, but not always.

Discussion in 'Physics' started by sgower, Mar 1, 2017.

  1. sgower

    sgower

    Joined:
    Mar 9, 2015
    Posts:
    316
    I'm seeing some very odd Physics.Raycast behavior where the ray is not consistently hitting a sphere collider.
    But what's really strange is that this inconsistent results seem to be related to creating and deleting gameobjects.

    See the behavior here:


    What I discovered is that the inconsistent behavior (seen in the video) goes away if I create and they destroy a sphere primitive gameobject immediately before calling Physics.Raycast. Here's the code including the workaround.

    Code (CSharp):
    1.      protected override void Update(){
    2.             //If I create and destroy a gameObject before calling Physics.Raycast, then
    3.             //The Raycast below always hits correctly.
    4.             GameObject cubeObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
    5.             cubeObject.transform.position = new Vector3(100f, 100f, 100f);
    6.             Destroy(cubeObject);
    7.  
    8.             //If the above code is removed, the following Raycast sometimes
    9.             //passes through sphere colliders
    10.             Ray pointerRaycast = new Ray(transform.position, transform.forward);
    11.             bool rayHit = Physics.Raycast(pointerRaycast, out pointerCollidedWith, Mathf.Infinity);
    12.        }
    13.  

    Obviously this is a bad workaround so I'd like to understand the correct way to resolve this. Does anyone have any idea what can explain this?

    I'm using Unity 5.6b9
     
  2. sgower

    sgower

    Joined:
    Mar 9, 2015
    Posts:
    316
    If I only create and delete the cube every 5 frames, then the output looks like this:

    rayHit=False
    rayHit=False
    rayHit=False
    rayHit=False
    rayHit=True
    rayHit=False
    rayHit=False
    rayHit=False
    rayHit=False
    rayHit=True

    So Physics.Raycast doesn't work at all for a few seconds unless I create a new GameObject every frame. But I only need to do this for 3-4 seconds, then the Racast hits every time. It seems that creating a new GameObject in the scene is causing something to happen that allows the Raycast to work. I'm sure someone can explain this, but I'm mystified.

    Also, I've tried calling the Raycast in both Update and FixedUpdate and see the same inconsistent behavior for both.
     
  3. DanJa512

    DanJa512

    Joined:
    Dec 14, 2015
    Posts:
    18
    I know this is super old, but I just spent a long time figuring out that any kind of raycast to hit a collider attached to an object with a non-kinematic rigidbody AND A NAVMESH AGENT will return extremely inconsistent hits. The rigidbody should be kinematic in this case.
     
    Krossend and joliveira21 like this.
  4. WidmerNoel

    WidmerNoel

    Joined:
    Jun 3, 2014
    Posts:
    66
    You're exact same answer is literally on all questions I find about raycast inconsistencies :D But it doesn't help in my case because no Rigidbodies are involved :) The raycast still hits in only about 93% of all cases :(
     
  5. tanerismail12345

    tanerismail12345

    Joined:
    Jun 21, 2021
    Posts:
    1
    It fixed my issue by just clicking kinematic on and then off, if i leave kinematic on, will that not ruin gravity and physics?