134 lines
4.1 KiB
C#
134 lines
4.1 KiB
C#
using BigSpace.Logic;
|
||
using BigSpace.XRCore.Event;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
public class RayCtr : MonoBehaviour
|
||
{
|
||
private float rayLength = 50f; //射线长度
|
||
public bool starRay = false; //是否开始射线
|
||
public LayerMask detectionLayer ; //检测的层级
|
||
public int myhandType; //自己手的类型1:左 2:右
|
||
public bool isstarRay = false;
|
||
|
||
|
||
//计时相关
|
||
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);
|
||
GlobalEventMgr.Listen<int>(GameEvent.EventRayEnd, GameDataManage_EventRayEnd);
|
||
GlobalEventMgr.Listen<int>(GameEvent.EventRayEnd, GameDataManage_EventRayEnd2);
|
||
//// 创建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 (!isstarRay) { return; }
|
||
// 在场景视图中绘制射线(仅用于调试)
|
||
if (!SceneMgr.Instance.isTrigger)
|
||
{
|
||
if (starRay)
|
||
{
|
||
Debug.DrawRay(transform.position, transform.forward * rayLength, Color.red);
|
||
|
||
// 从物体位置向前方发射射线
|
||
Ray ray = new Ray(transform.position, 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;
|
||
}
|
||
}
|
||
void GameDataManage_EventRayEnd(int type)
|
||
{
|
||
isstarRay = true;
|
||
}
|
||
void GameDataManage_EventRayEnd2(int type)
|
||
{
|
||
isstarRay = false;
|
||
}
|
||
}
|