Files
PrinceOfGlory/Assets/Scripts/Scene_02/RayCtr.cs
kridoo 0ba6ef05dd 1
2025-11-19 14:48:52 +08:00

133 lines
4.0 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 BigSpace.Logic;
using BigSpace.XRCore.Event;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class RayCtr : MonoBehaviour
{
private float rayLength = 50f; //射线长度
public bool starRay = false; //是否开始射线
public LayerMask detectionLayer ; //检测的层级
public int myhandType; //自己手的类型1左 2
//计时相关
bool isRun = false;
float waitTime = 1f;
float runTime = 0;
int fontIndex = 0; //显示的字
//public Color rayColor = Color.blue;
//public float startWidth = 0.01f;
//public float endWidth = 0.01f;
//private LineRenderer lineRenderer;
// Start is called before the first frame update
void Start()
{
GlobalEventMgr.Listen<int>(GameEvent.EventRayStart, GameDataManage_EventRayStart);
//// 创建LineRenderer组件
//lineRenderer = gameObject.AddComponent<LineRenderer>();
//// 设置材质
//lineRenderer.material = new Material(Shader.Find("Unlit/Color"));
//lineRenderer.material.color = rayColor;
//// 设置宽度
//lineRenderer.startWidth = startWidth;
//lineRenderer.endWidth = endWidth;
//// 设置两点(起点和终点)
//lineRenderer.positionCount = 2;
}
//void UpdateVisualRay()
//{
// // 更新射线起点和终点
// lineRenderer.SetPosition(0, transform.position);
// // 射线检测
// if (Physics.Raycast(transform.position, transform.forward, out RaycastHit hit, rayLength,detectionLayer))
// {
// lineRenderer.SetPosition(1, hit.point);
// // 碰撞时改变颜色
// lineRenderer.endColor = Color.red;
// }
// else
// {
// lineRenderer.SetPosition(1, transform.position + transform.forward * rayLength);
// lineRenderer.endColor = rayColor;
// }
//}
// Update is called once per frame
void Update()
{
//UpdateVisualRay(); //显示射线
// 在场景视图中绘制射线(仅用于调试)
if (!SceneMgr.Instance.isTrigger)
{
if (starRay)
{
Debug.DrawRay(transform.position, transform.forward * rayLength, Color.red);
Ray ray;
if (SceneMgr.Instance.nowSceneId == 2)
{
ray = new Ray(transform.position, transform.forward);
}
else
{
var Cam = Camera.main;
ray = new Ray(Cam.transform.position, Cam.transform.forward);
}
// 从物体位置向前方发射射线
RaycastHit hit; // 存储射线碰撞信息
//Debug.Log("调用了射线......第1步");
if (Physics.Raycast(ray, out hit, rayLength, detectionLayer))
{
GameObject hitObject = hit.collider.gameObject; // 获取碰撞到的物体
//Debug.Log("检测到物体: " + hitObject.name+"||"+ hitObject.name.Substring(0, 4)); // 输出物体名称
if ("Quad" == hitObject.name.Substring(0, 4))
{
fontIndex = int.Parse(hitObject.name.Substring(4));
Debug.Log("对了第几个文字......" + fontIndex);
starRay = false;
isRun = false;
runTime = 0;
GlobalEventMgr.Dispatch(GameEvent.EventRayQuadOk, true, myhandType, fontIndex);
}
}
}
}
if (isRun)
{
runTime += Time.deltaTime;
if (runTime >= waitTime)
{
isRun = false;
starRay = false;
runTime = 0;
}
}
}
void GameDataManage_EventRayStart(int type)
{
//Debug.Log("调用了:"+type);
if (myhandType == type)
{
starRay = true;
isRun = true;
runTime = 0;
}
}
}