Raycast don't ignore player layermask

Hi!

I’m trying to understand raycasts. I’m trying to shoot with ignoring the player layermask. I did everything exactly like described here but it don’t work.

Part of my code

	void Shoot () {
		RaycastHit hit;
		int layerMask = 1 << 8;
		layerMask = ~layerMask;
		if (Physics.Raycast (cam.transform.position, cam.transform.forward, out hit, range, layerMask)) {

			Target hitTarget = hit.transform.GetComponent<Target> ();
			if (hitTarget != null) {
				hitTarget.TakeDamage (damage);
				GameObject impact = Instantiate (hitTarget.impactEffect, hit.point, Quaternion.LookRotation (hit.normal)) as GameObject;
				Destroy (impact, 2f);
			} else {
				GameObject impact = Instantiate (impactEffect, hit.point, Quaternion.LookRotation (hit.normal)) as GameObject;
				Destroy (impact, 2f);
			}

			if (hit.rigidbody != null) {
				hit.rigidbody.AddForce (-hit.normal * damage * 50f);
			}

		}
		currentAmmo--;
		muzzleFlash.Play ();
	}

EDIT: I just tried to put the player on the “Ignore Raycast” layer. Still doesn’ work. The Player has a Capsule Collider and a Rigidbody

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.

You have to understand shifting :slight_smile: its operator is “<<”
After u understood shifting here is your solution