_xiaofang/xiaofang/Assets/Script/Item/VaultWall.cs

110 lines
3.1 KiB
C#
Raw Normal View History

2024-10-21 10:08:49 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VaultWall : MonoBehaviour
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>Է<EFBFBD>Խ
public bool isPlayerInRange = false;
public GameObject Sigh; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Quad <20>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public float blinkSpeed = 2.0f; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˸<EFBFBD><CBB8><EFBFBD>ٶ<EFBFBD>
private Material[] sighMaterials; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD>
private Color[] originalColors; // <20><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭʼ<D4AD><CABC>ɫ
private bool isBlinking = false;
private Coroutine blinkCoroutine; // <20><><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD>Э<EFBFBD>̵ľ<CCB5><C4BE><EFBFBD>
private void Start()
{
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> MeshRenderer <20><><EFBFBD><EFBFBD>
MeshRenderer[] renderers = Sigh.GetComponentsInChildren<MeshRenderer>();
if (renderers.Length > 0)
{
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ʺ<EFBFBD><CABA><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>
sighMaterials = new Material[renderers.Length];
originalColors = new Color[renderers.Length];
for (int i = 0; i < renderers.Length; i++)
{
sighMaterials[i] = renderers[i].material; // ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD>ȷ<EFBFBD><C8B7>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2>ʶ<EFBFBD><CAB6><EFBFBD>
originalColors[i] = sighMaterials[i].color; // <20><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭʼ<D4AD><CABC>ɫ
}
}
else
{
Debug.LogError("No MeshRenderer found in the children of Sigh.");
}
}
// <20><><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EBB4A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player")) // ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD>ɫ
{
isPlayerInRange = true;
StartBlinking(); // <20><><EFBFBD><EFBFBD><EFBFBD>ҽ<EFBFBD><D2BD>Χʱ<CEA7><CAB1>ʼ<EFBFBD><CABC>˸
}
}
// <20><><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EBBFAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
isPlayerInRange = false;
StopBlinking(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBBFAA>Χʱֹͣ<CDA3><D6B9>˸
}
}
public void StartBlinking()
{
if (!isBlinking) // <20><>ֹ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD>
{
isBlinking = true;
blinkCoroutine = StartCoroutine(BlinkAlpha());
}
}
public void StopBlinking()
{
if (isBlinking) // <20><>ֹ<EFBFBD>ظ<EFBFBD>ֹͣЭ<D6B9><D0AD>
{
isBlinking = false;
if (blinkCoroutine != null)
{
StopCoroutine(blinkCoroutine); // ʹ<><CAB9>Э<EFBFBD>̾<EFBFBD><CCBE><EFBFBD><EFBFBD><EFBFBD>ȫֹͣ
}
// <20><>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭʼ<D4AD><CABC>ɫ
for (int i = 0; i < sighMaterials.Length; i++)
{
sighMaterials[i].color = originalColors[i];
}
}
}
private IEnumerator BlinkAlpha()
{
float elapsedTime = 0f;
while (isBlinking)
{
// ʹ<><CAB9> Mathf.Sin ʹ Alpha <20><> 0 <20><> 1 ֮<><D6AE>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
elapsedTime += Time.deltaTime * blinkSpeed;
float alphaValue = (Mathf.Sin(elapsedTime) + 1) / 2; // sin <20>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD> -1 <20><> 1 ֮<>䣬ת<E4A3AC><D7AA><EFBFBD><EFBFBD> 0 <20><> 1 ֮<><D6AE>
for (int i = 0; i < sighMaterials.Length; i++)
{
Color color = sighMaterials[i].color;
color.a = alphaValue; // ƽ<><C6BD><EFBFBD>޸<EFBFBD> Alpha ֵ
sighMaterials[i].color = color;
}
yield return null; // <20>ȴ<EFBFBD><C8B4><EFBFBD>һ֡
}
}
}