Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Odd behaviour with SphereCasting and bitlayers. Potential Unity bug.

Discussion in 'Scripting' started by DolphinsCydonia, Jul 25, 2011.

  1. DolphinsCydonia

    DolphinsCydonia

    Joined:
    Jul 25, 2011
    Posts:
    11
    In my game I have a auto-locking feature where the hud crosshairs will snap to appropriate targets. It works by calling a Physics.SphereCastAll as such:

    var hitInfos : RaycastHit[] = Physics.SphereCastAll(rayCentre.origin, reticuleAutoAimRadius, rayCentre.direction);

    Now this has been fine until recently when a very large terrain map was created for the game. Calling this code will lower FPS to 1 and stall the game for a brief period each time the function is executed.

    I decided to use bit layers to selectively ignore all other objects except ones on a Layer I called "Reticule" which I placed in User Layer 8. I expect Physics.SphereCastAll to only return results from objects in this layer.

    Thus:

    private var reticuleLayerBitMask = (1 << 8);

    and later,

    var hitInfos : RaycastHit[] = Physics.SphereCastAll(rayCentre.origin, reticuleAutoAimRadius, rayCentre.direction, reticuleLayerBitMask);

    This is where it starts to get odd.

    The game now runs very smoothly. However the SphereCast will STILL collect collision information from the terrain even though the terrain is not on the Reticule Layer.

    How is the game running smoothly if it is collecting information on the Terrain still? And why are inappropriate objects not being ignored?