射线触发UI

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

public class camTrigger : MonoBehaviour {

    public Transform rayCam;
    public Transform sphere;

    bool isFinding = true;
	// Update is called once per frame
	void Update () {

        Ray ray = new Ray(rayCam.position, rayCam.forward);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            if(hit.collider.tag == "trigger")
            {
                isFinding = true;
                hit.collider.gameObject.GetComponent<MeshRenderer>().enabled = true;
            }
            else
            {
                if(isFinding == true)
                {
                    GameObject[] gg = GameObject.FindGameObjectsWithTag("trigger");
                    foreach (var item in gg)
                    {
                        item.GetComponent<MeshRenderer>().enabled = false;
                        //print("trigger : " + item.name);
                    }
                    isFinding = false;
                }
            }
        }
        sphere.position = hit.point;
        Debug.DrawLine(rayCam.position, hit.point, Color.red);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_33174548/article/details/85622351