开灯
当点击时触发开灯效果
(不用设置触发器)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class OpenLight : MonoBehaviour
{public Transform light;bool isOpen;private void OnMouseDown(){if (!isOpen){light.GetComponent<Light>().enabled = true;isOpen=true;}else{light.GetComponent<Light>().enabled = false;isOpen = false;}}
}
宝箱
开宝箱
制作动画
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class LidOpen : MonoBehaviour
{public Transform chest; // 宝箱的动画控制器private void OnMouseDown(){// 播放宝箱打开动画chest.GetComponent<Animator>().SetTrigger("OpenChest");//跑酷if (chest.name == "RunBox"){StartCoroutine(DelayedLoadScene("Run", 3f)); }//翻牌else if (chest.name == "DrawBox"){StartCoroutine(DelayedLoadScene("Draw", 3f)); }/* else if (chest.name == "PuzzleBox"){StartCoroutine(DelayedLoadScene("Puzzle", 3f));}*///拼图的跳转else if (chest.name == "PuzzleBox"){SceneManager.LoadScene(3);}}IEnumerator DelayedLoadScene(string sceneName, float delay){yield return new WaitForSeconds(delay); // 等待指定的延迟时间SceneManager.LoadScene(sceneName); // 加载指定名称的场景}}