using System.Collections; using System.Collections.Generic; using UnityEngine; public class Raycast_control : MonoBehaviour { public Camera main; public GameObject hitgo; public RaycastHit hitInfo; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() {if (Input.GetMouseButton(0)) { Ray ray = main.ScreenPointToRay(Input.mousePosition); //声明一个Ray结构体,用于存储该射线的发射点,方向 //声明一个RaycastHit结构体,存储碰撞信息 if (Physics.Raycast(ray, out hitInfo, 10000, 1 << 7)) { // Debug.Log(hitInfo.collider.gameObject.name); hitgo = hitInfo.collider.gameObject; Debug.DrawRay(ray.origin, ray.direction, Color.red); //这里使用了RaycastHit结构体中的collider属性 //因为hitInfo是一个结构体类型,其collider属性用于存储射线检测到的碰撞器。 //通过collider.gameObject.name,来获取该碰撞器的游戏对象的名字。 } else { hitgo = null; } } else { hitgo = null; } } }