Different approach to raycast target

Hi i could we use the raycast to check if we hit a specific layer something like this

private void InteractWithCombat()
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            bool hasHit = Physics.Raycast(ray, out hit, LayerMask.GetMask("Enemy"));
            if (hasHit)
            {
                GetComponent<Fighter>().Fight();
            }
        }

or would it cause some problerms later in the course?

First of all, this is a good use of layer masks! You’re right, if we were only planning on raycasting for the enemies, this is an excellent solution, and I do not want to discourage your finding alternate approaches.
Unfortunately, this likely will cause a problem later in the course when we switch from an InteractWithCombat() model to performing a raycast and allowing the objects it intersects with to handle the raycast themselves (enemies, items on the ground, in later courses quest givers, etc).
At that point, you’ll need to either remove the layer mask or add the layer masks for those items.)

1 Like

Privacy & Terms