game(scripts): selective recommit 72e2d20

This commit is contained in:
Mark Joshwel 2025-02-11 15:09:12 +08:00
parent fdbf71f789
commit a688daa3dd
6 changed files with 67 additions and 0 deletions

View file

@ -0,0 +1,56 @@
/*
Author : Wai Lam
Date : 11/2/2025
Description : Car obstacle
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CanvasFade : MonoBehaviour
{
public CanvasGroup fadeCanvasGroup; // Assign in Inspector
public float fadeDuration = 1f; // Duration for fade in/out
public float displayDuration = 5f; // Time the UI stays fully visible
public string nextSceneName; // Name of the scene to load
private bool hasTriggered = false; // Prevent multiple triggers
private void OnTriggerEnter(Collider other)
{
// Check if the player entered the trigger
if (!hasTriggered && other.CompareTag("Player"))
{
hasTriggered = true;
StartCoroutine(FadeInAndLoadScene());
}
}
IEnumerator FadeInAndLoadScene()
{
// Fade In
yield return StartCoroutine(Fade(0f, 1f, fadeDuration));
// Display UI for 5 seconds
yield return new WaitForSeconds(displayDuration);
// Load the next scene
SceneManager.LoadScene(nextSceneName);
}
IEnumerator Fade(float startAlpha, float endAlpha, float duration)
{
float elapsed = 0f;
fadeCanvasGroup.alpha = startAlpha;
while (elapsed < duration)
{
elapsed += Time.deltaTime;
fadeCanvasGroup.alpha = Mathf.Lerp(startAlpha, endAlpha, elapsed / duration);
yield return null;
}
fadeCanvasGroup.alpha = endAlpha;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5bae7a3fb14a3854ca6668491a8257b9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: