How to make the raycast ignore its parent?

The raycast from my gun start at the main câmera. But if the angle is too low (if i look to the floor), the raycast hits the player. If i keep shooting at the floor, i die (whatheheck). I was previously raycasting from an empty GameObject in front of the camera, and was working just fine.

My script just in case:

if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out ray, range)) {

    life = ray.transform.GetComponent<life>();
    if (life != null) {
        life.TakeDamage(damage);
    }

rigidbuddy = raio.transform.GetComponent<Rigidbody>();
if (rigidbuddy != null) {
    rigidbuddy.AddForce(-transform.forward * 40 * danodaarma);
}

}

Usual everyday stuff. I don’t know why such a bug happens.

2 Answers

2

just turn off queries start in colliders and it ignore the collider when cast start inside of it

THIS! The original question is not related to my issue, but if anyone is having trouble with a player controller reacting in a weird way when the player's layermask is set to collide with its own layer, then setting queries off is the solution. I hope this can help someone else out. You can turn this setting off through Edit -> Project Settings -> Physics2D -> Queries Start In Colliders.

You can pass a layermask in the Pysics.Raycast method. Create a Player layer and set the layermask to ignore the player.