jozza710
jozza710

Reputation: 95

Raycast that ignore certain Game Objects

PlayerController Class

public LayerMask groundLayer;

    void LookRotation()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hitInfo;
    if (Physics.Raycast(ray, out hitInfo, groundLayer))
    {
        destination = new Vector3(hitInfo.point.x, transform.position.y, hitInfo.point.z);
        //Look at mouse position
        transform.LookAt(destination);
    }
}

I am having an issue getting my Raycast to either ignore my player or only detect the ground. In the actual unity inspector I have set the LayerMask to be my Ground Layer and with my current code it will only detect the ground if it is in the Ground Layer although the player will still interfere with the Raycast even though it is not in the Ground Layer. I need the Raycast to completely ignore the player and even though this is achieved by adding the player to the Ignore Raycast layer, I can't do that because the player will need to be detected by different Raycasts. all of this code occurs in my PlayerController script as it is used to determine my players rotation, I am unsure if that may be the reason the player interferes with the Raycast. Cheers in advance!

Upvotes: 0

Views: 1134

Answers (1)

Fattie
Fattie

Reputation: 12277

(1) Are you using the layer MASK the correct way?

It's more like this ...

int layerMaskYourHero = 1 << LayerMask.NameToLayer("YourHero");

and then

    RaycastHit2D hit = Physics2D.Raycast(
        laserEmittingPoint.position, Vector2.left,
        castLength,
        layerMaskYourHero);

(2) Say, you're using the physics matrix right ?

enter image description here

go to Edit -> Project Settings -> Physics

Upvotes: 2

Related Questions