ShanxiKnowledgeBase/SXElectricalInspection/Assets/Script/raycast/Raycast_control_qianxing.cs

49 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Raycast_control_qianxing : 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 << 8))
{
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;
}
}
}
}