|
Hello, I am using the FPS tutorial from Unity. I am using the machine gun script and I want to ignore a specific collider (Element 7 tag - want to ignore). Here is the machine gun script, I've added the layerMask, but its still not ignoring the 7th tag which is on the collider I want to ignore?!? Any ideas?
(comments are locked)
|
|
If you want power over raycasting different layers you can selectively choose which layers are affected by the raycast. If you are only wanting to exclude one object then move that object to the ignore raycast layer such as the bottom of this post. Sometimes this may or may not work so knowing the proper technique WILL come in handy eventually. Casting Rays Selectively Using layers you can cast rays and ignore colliders in specific layers. For example you might want to cast a ray only against the player layer and ignore all other colliders. The Physics.Raycast function takes a bitmask, where each bit determines if a layer will be ignored or not. If all bits in the layerMask are on, we will collide against all colliders. If the layerMask = 0, we will never find any collisions with the ray. // bit shift the index of the layer to get a bit mask var layerMask = 1 << 8; // Does the ray intersect any objects which are in the player layer. if (Physics.Raycast (transform.position, Vector3.forward, Mathf.Infinity, layerMask)) print ("The ray hit the player"); In the real world you want to do the inverse of that however. We want to cast a ray against all colliders except those in the Player layer. function Update () { // Bit shift the index of the layer (8) to get a bit mask var layerMask = 1 << 8; // This would cast rays only against colliders in layer 8. // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask. layerMask = ~layerMask; var hit : RaycastHit; // Does the ray intersect any objects excluding the player layer if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, Mathf.Infinity, layerMask)) { Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) * hit.distance, Color.yellow); print ("Did Hit"); } else { Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) *1000, Color.white); print ("Did not Hit"); } } strong text When you don't pass a layerMask to the Raycast function, it will only ignore colliders that use the IgnoreRaycast layer. This is the easiest way to ignore some colliders when casting a ray.
(comments are locked)
|
|
Just put the collider you'd like to ignore on the "IgnoreRaycast" layer.
(comments are locked)
|
