x


Help With Ignoring Collider With Raycast!?

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?

function FireOneShot () {
    var direction = transform.TransformDirection(Vector3.forward);
    var hit : RaycastHit;
    var localOffset = transform.position + transform.up * 2;
    var layerMask = 1 << 7;
    layerMask = ~layerMask;

    // Did we hit anything?
    //transform.position + transform.up * 10
    if (Physics.Raycast (localOffset, direction, hit, range, layerMask) && MachineGun == true) {
         Debug.DrawLine (localOffset, hit.point, Color.yellow);
        // Apply a force to the rigidbody we hit
        if (hit.rigidbody)
            hit.rigidbody.AddForceAtPosition(force * direction, hit.point);

        // Place the particle system for spawing out of place where we hit the surface!
        // And spawn a couple of particles
        if (hitParticles) {
            hitParticles.transform.position = hit.point;
            hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
            hitParticles.Emit();
        }

        // Send a damage message to the hit object          
        hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
    }

    bulletsLeft--;

    // Register that we shot this frame,
    // so that the LateUpdate function enabled the muzzleflash renderer for one frame
    m_LastFrameShot = Time.frameCount;
    enabled = true;

    // Reload gun in reload Time        
    if (bulletsLeft == 0)
        Reload();           
}
more ▼

asked Apr 01 '11 at 12:48 PM

oliver-jones gravatar image

oliver-jones
2.5k 205 225 253

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

link text

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.

more ▼

answered Apr 01 '11 at 02:45 PM

DcGoD gravatar image

DcGoD
31 1 1 4

(comments are locked)
10|3000 characters needed characters left

Just put the collider you'd like to ignore on the "IgnoreRaycast" layer.

more ▼

answered Apr 01 '11 at 12:54 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1682
x321
x67
x42

asked: Apr 01 '11 at 12:48 PM

Seen: 5640 times

Last Updated: Apr 01 '11 at 12:48 PM