unity 射线检测,鼠标点击3D物体交互

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

public class NewBehaviourScript : MonoBehaviour
{
Ray ray;
RaycastHit hit;
GameObject obj;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log(“点击鼠标左键”);

        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit))
        {
            Debug.Log(hit.collider.gameObject.name);
            obj = hit.collider.gameObject;
            //通过名字
            if (obj.name.Equals("BeiJiuChuan"))
            {
                Debug.Log("点中" + obj.name);
            }
            //通过标签
            if (obj.tag == "move")
            {
                Debug.Log("点中" + obj.name);
            }
        }
    }
}

}

猜你喜欢

转载自blog.csdn.net/qq_42986916/article/details/125554576
今日推荐