How to set layer mask only on terrain trees?

Viewed 25

I wanna use Physic.Raycast with layerMask option. In editor, i can only set layer mask of whole terrain, not only trees. Setting layer mask on trees prefabs doesn't work too. What should i do? Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RayThings : MonoBehaviour
{
    public GameObject cam2;
    [SerializeField] LayerMask mask;
    public Camera cam;
    private Ray ray;
    private RaycastHit hit;
    [SerializeField] private float maxDistanceRay;
    private void Update(){
        Ray();
        DrawRay(); 
    }
    private void Ray() {
        ray = cam.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
    }
    private void DrawRay() {
        if (Physics.Raycast(ray, out hit, maxDistanceRay, mask)) {
            Debug.DrawRay(ray.origin, ray.direction * maxDistanceRay, Color.blue);
            cam2.transform.position = hit.point;
        }
        if (hit.transform == null) cam2.transform.position = new Vector3(999f, 999f, 999f);

    }
} 
0 Answers
Related