unity 射线拾取物体

  using UnityEngine;
  using System.Collections;
  
  public class RayTest : MonoBehaviour {
  
      // Use this for initialization
      void Start () {
      
      }
     
     // Update is called once per frame
     void Update () 
     {
         if(Input.GetMouseButton(0))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//从摄像机发出到点击坐标的射线
             RaycastHit hitInfo;
             if(Physics.Raycast(ray,out hitInfo))
             {
                 Debug.DrawLine(ray.origin,hitInfo.point);//划出射线,只有在scene视图中才能看到
                 GameObject gameObj = hitInfo.collider.gameObject;
                 Debug.Log("click object name is " + gameObj.name);
                 if(gameObj.tag == "boot")//当射线碰撞目标为boot类型的物品 ,执行拾取操作
                 {
                     Debug.Log("pick up!");
                 }
             }
         }
     }
 }

猜你喜欢

转载自blog.csdn.net/qq_35051525/article/details/79037709
今日推荐