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