Files
PrinceOfGlory/Assets/__Test/testContro.cs
kridoo 6e91a0c7f0 111
2025-09-15 17:32:08 +08:00

149 lines
5.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testContro : MonoBehaviour
{
public GameObject came;
private int m_mask = 14;
private void OnTriggerEnter(Collider other)
{
//Debug.Log("Trigger进来了...................." + other.gameObject.name);
if (other.CompareTag("MainCamera"))
{
//Debug.Log("1111111111");
Transform camera = other.transform;
Ray m_fRay = new Ray(camera.position, camera.forward);
Ray m_rRay = new Ray(camera.position, camera.right);
Ray m_lRay = new Ray(camera.position, -camera.right);
Ray m_bRay = new Ray(camera.position, -camera.forward);
RaycastHit hitInfo;
//使用检测层,进行射线检测
if (Physics.Raycast(m_fRay, out hitInfo, 2, 1 << m_mask) ||
Physics.Raycast(m_rRay, out hitInfo, 2, 1 << m_mask) ||
Physics.Raycast(m_lRay, out hitInfo, 2, 1 << m_mask) ||
Physics.Raycast(m_bRay, out hitInfo, 2, 1 << m_mask))
{
Debug.Log("进去了......................");
//进入边界后,光标显示关闭
//ArrowManager.SingleTon.SetTarget(null);
//to do: 进入边界后要做的逻辑
Camera.main.cullingMask = ~(1 << 13);
}
}
}
private void OnTriggerStay(Collider other)
{
//Debug.Log("Trigger一直在.................:" + other.gameObject.name);
//if (other.CompareTag("MainCamera"))
//{
// Debug.Log("Trigger一直在.................:" + other.gameObject.name);
//}
}
private void OnTriggerExit(Collider other)
{
//Debug.Log("Trigger退出谁了:" + other.gameObject.name);
if (other.CompareTag("MainCamera"))
{
//Debug.Log("222222222");
Transform camera = other.transform;
Ray m_fRay = new Ray(camera.position, camera.forward);
Ray m_rRay = new Ray(camera.position, camera.right);
Ray m_lRay = new Ray(camera.position, -camera.right);
Ray m_bRay = new Ray(camera.position, -camera.forward);
RaycastHit hitInfo;
//使用检测层,进行射线检测
if (Physics.Raycast(m_fRay, out hitInfo, 2, 1 << m_mask) ||
Physics.Raycast(m_rRay, out hitInfo, 2, 1 << m_mask) ||
Physics.Raycast(m_lRay, out hitInfo, 2, 1 << m_mask) ||
Physics.Raycast(m_bRay, out hitInfo, 2, 1 << m_mask))
{
Debug.Log("出来了...............");
//光标显示
//ArrowManager.SingleTon.SetTarget(m_center);
//to do: 执行出边界后,需要做的逻辑
Camera.main.cullingMask = 1 << 13;
}
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
Check();
}
}
void Check()
{
Transform camera = came.transform;
Ray m_fRay = new Ray(camera.position, camera.forward);
Ray m_rRay = new Ray(camera.position, camera.right);
Ray m_lRay = new Ray(camera.position, -camera.right);
Ray m_bRay = new Ray(camera.position, -camera.forward);
RaycastHit hitInfo;
//使用检测层,进行射线检测
if (Physics.Raycast(m_fRay, out hitInfo, 50, 1 << m_mask) ||
Physics.Raycast(m_rRay, out hitInfo, 50, 1 << m_mask) ||
Physics.Raycast(m_lRay, out hitInfo, 50, 1 << m_mask) ||
Physics.Raycast(m_bRay, out hitInfo, 50, 1 << m_mask))
{
Debug.Log("在外面...............");
//光标显示
//ArrowManager.SingleTon.SetTarget(m_center);
//to do: 执行出边界后,需要做的逻辑
//Camera.main.cullingMask = 1 << 13;
}
else
{
Debug.Log("在里面...............");
}
}
//private bool IsInPologyArea(Vector3 curPos, Vector3 direction, int layer, float hitHeight = 0.5f, string tag = "")
//{
// int maxCheckCount = 20;
// int curCheckCount = 0;
// int hitCount = 0;
// Vector3 curCheckPos = new Vector3(curPos.x, hitHeight, curPos.z);
// direction.y = 0; //水平方向检测
// Vector3 curDir = direction.normalized / 100;
// RaycastHit hit;
// Ray ray = new Ray(curCheckPos, curDir);
// bool state = Physics.Raycast(ray, out hit, 50.0f, 1 << layer);
// // Debug.Log($"collider first: curCheckPos: {curCheckPos}, direction: {curDir}, hit.point: {hit.point}, hit.collider.tag: {hit.collider.tag}");
// while (state)
// {
// // Debug.Log($"hit.collider curCheckPos: {curCheckPos}, direction: {curDir}, hit.point: {hit.point}, hit.collider.tag: {hit.collider.tag}");
// if (tag == "" || hit.collider.tag == tag)
// {
// hitCount += 1;
// }
// // 推进射线
// curCheckPos = new Vector3(hit.point.x + curDir.x, hitHeight, hit.point.z + curDir.z);
// ray = new Ray(curCheckPos, curDir);
// state = Physics.Raycast(ray, out hit, 50.0f, 1 << layer);
// curCheckCount += 1;
// if (curCheckCount >= maxCheckCount)
// {
// break;
// }
// }
// Debug.Log($"hitCount: {hitCount}, layer: {layer}, tag: {tag}");
// return hitCount % 2 == 1;
//}
}