用Unity3D从摄像机到鼠标的射线,也就是鼠标检测到Unity3D世界中的游戏对象!

  如何才能使射线检测到游戏对象呢,只有一个条件,那就是必须添加collider组件,否则你是绝度检测不到,你想要检测的物体的!
/***
*	Title:"XXX" 项目
*		主题:XXX
*	Description:
*		功能:XXX
*	Date:2017
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

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

namespace SimpleUIFrame
{
	public class Test_GetBtnTextContent : MonoBehaviour
	{
        private Camera UICamera;

        public Text DisplayBtnText;                     //显示文本组件

        private GameObject go;                          //鼠标点击的物体

		void Start()
		{
           
		}

        private void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(ray,out hit))
                {
                    go = hit.collider.gameObject;
                    DisplayBtnText.text = go.name;


                }
            }
        }

        
    }
}


参考:https://blog.csdn.net/violettd/article/details/49622241

猜你喜欢

转载自blog.csdn.net/xiaochenXIHUA/article/details/80515409