using System.Collections; using System.Collections.Generic; using UnityEngine; public class Fire : MonoBehaviour { private GameObject FireEffect; private bool isPlayerInRange = false; // 检查玩家是否在范围内 private bool isExtinguishing = false; // 是否正在灭火 public float extinguishTime = 3.0f; // 灭火所需的时间 private float holdTime = 0f; // 按住按钮的计时 private CharacterControl characterControl;//玩家控制器 void Start() { FireEffect = transform.Find("FireEffect").gameObject; } void Update() { // 检查玩家是否在范围内且按住灭火键(假设是 "E" 键) if (isPlayerInRange && Input.GetMouseButton(0)) { // 禁用玩家控制器,防止玩家移动 if (!isExtinguishing) DisablePlayerControl(); holdTime += Time.deltaTime; Debug.Log("灭火计时:" + holdTime); if (holdTime >= extinguishTime && !isExtinguishing) { // 如果玩家按住键达到指定时间,停止火焰 StopFire(); isExtinguishing = true; // 防止重复触发灭火 } } else { // 如果玩家松开按键或者离开范围,重置计时 holdTime = 0f; isExtinguishing = false; // 恢复玩家控制器 if (!Input.GetMouseButton(0)) EnablePlayerControl(); } } // 开始火焰 public void StartFire() { FireEffect.SetActive(true); isExtinguishing = false; // 重置灭火状态 } // 停止火焰 public void StopFire() { FireEffect.SetActive(false); Debug.Log("火焰已被熄灭"); } // 当玩家进入火焰范围时触发 private void OnTriggerEnter(Collider other) { if (other.CompareTag("Player")) { isPlayerInRange = true; // 玩家进入范围 characterControl = other.GetComponent(); } } // 当玩家离开火焰范围时触发 private void OnTriggerExit(Collider other) { if (other.CompareTag("Player")) { isPlayerInRange = false; // 玩家离开范围 characterControl = null; } } // 禁用玩家控制器 private void DisablePlayerControl() { if (characterControl != null) { characterControl.enabled = false; } } // 启用玩家控制器 private void EnablePlayerControl() { if (characterControl != null) { characterControl.enabled = true; } } }