I’ve got a spaceship with lasers. It’s surrounded by a force field. When I use raycasts to determine if my laser hits anything, I of course my myself.
How can I avoid that? Putting the force field into the ignoreraycast layer doesn’t seem to do it, even when I do it only on my own player object, at least tests show that then I suddenly don’t hit other players anymore.
var layerMask = 1 << 8; //layer 8 is Player Layer
// 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 detectorPosition = transform.position;
var Detector : RaycastHit;
if (Physics.Raycast (detectorPosition, -Vector3.up, Detector,Mathf.Infinity,layerMask))
{
//do some stuff
}
No, the problem with the player layer, as I understand it, is that both me and the other players will be in the player layer. So I will hit either them and me, or neither.
I want to test against OTHER players, but not myself.
Just before you do the raycast, move the current player into another layer and ignore that layer during the raycast. Then move them back into their usual layer immediately afterwards.
My solution was to raycast from transform.position + Vector3.forward*ShieldDistance
And now, even when I see the beam I create for the laser visual effect to clearly pass right through the target object, and the same target object does react to rockets, the laster raycast doesn’t throw a “hit” message.
What’s going on?
function FireGuns() {
// Did the time exceed the reload time?
if (Time.time > reloadTime + lastShot) {
// raycast to see if we hit anything
var hit: RaycastHit;
var length;
if (Physics.Raycast(transform.position + Vector3.forward*ShieldDistance, Vector3.forward, hit, MaxDistance)) {
// we hit something, draw until collider, and tell it that it was hit
length = hit.distance;
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
print ("Hit " + hit.transform.name);
GameObject.Find("GameGUI").SendMessage("AddMessage", "You hit "+hit.transform.name+" with lasers.");
} else {
// we hit nothing, draw the full distance laser
length = MaxDistance;
}
target = Vector3(length, 0, 0);
line.SetPosition(1, target);
audio.Play();
renderer.enabled = true;
Invoke("TurnOff", timeOut);
lastShot = Time.time;
}
}