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.
protected override void Update(){
//If I create and destroy a gameObject before calling Physics.Raycast, then
//The Raycast below always hits correctly.
GameObject cubeObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
cubeObject.transform.position = new Vector3(100f, 100f, 100f);
Destroy(cubeObject);
//If the above code is removed, the following Raycast sometimes
//passes through sphere colliders
Ray pointerRaycast = new Ray(transform.position, transform.forward);
bool rayHit = Physics.Raycast(pointerRaycast, out pointerCollidedWith, Mathf.Infinity);
}
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