Gameplay.GlowController - robblofield/TomeboundDocs GitHub Wiki

Gameplay.GlowController

Author: Shahrzad Beevis
Last Updated: 14/05/2024

Overview

A script controlling the activation and deactivation of the glow effect for phase 2 of the first boss encounter.

Dependencies

N/A

Contents

  • Use case
  • Breakdown of code
    • Using Statements
    • Class Declaration
    • Variables
    • Method/Start()
    • Method/TurnEmissionOn()
    • Method/TurnEmissionOff()
    • Method/FadeEmission(float startIntensity, float endIntensity, float duration)
  • Future Expansion
  • Full Code Reference

Use case

This script can be used to enable and disable the glow effect smoothly based on in game events such as interactions or environmental changes

Breakdown of Code

Using Statements

using System.Collections;
using UnityEngine;

Class Declaration

public class GlowController : MonoBehaviour

Variables

private Renderer objectRenderer;
private Material glowMaterial;

private Renderer objectRenderer:

Stores the Renderer component attached to the GameObject.

private Material glowMaterial:

Stores the material associated with the Renderer, which controls the emission intensity.

Start()

private void Start()
{
    objectRenderer = GetComponent<Renderer>();

    if (objectRenderer != null)
    {
        glowMaterial = objectRenderer.material;
    }
    else
    {
        Debug.LogError("Renderer component not found!");
    }
}

This method initialises the GlowController by retrieving the Renderer component attached to the GameObject. If the Renderer component is found, its associated material is assigned to the glowMaterial variable. If the Renderer component is not found, an error message is logged to the console.

TurnEmissionOn()

 public void TurnEmissionOn()
 {
     if (glowMaterial != null)
     {
         StartCoroutine(FadeEmission(glowMaterial.GetFloat("_Opacity"), 0f, 2f));
     }
 }

This method activates the glow effect by gradually increasing the emission intensity of the material over a specified duration. It first checks if the glowMaterial variable is not null, indicating that a material is successfully assigned. If the material exists, the method initiates a coroutine named FadeEmission() to smoothly fade the emission intensity from its current value to 0, resulting in a glowing effect on the GameObject.

TurnEmissionOff()

public void TurnEmissionOff()
{
    if (glowMaterial != null)
    {
        StartCoroutine(FadeEmission(glowMaterial.GetFloat("_Opacity"), 10f, 4f)); 
    }
}

This method deactivates the glow effect by gradually decreasing the emission intensity of the material over a specified duration. Similar to the TurnEmissionOn() method, it verifies the existence of the glowMaterial variable. Upon confirmation, it invokes the FadeEmission() coroutine to smoothly decrease the emission intensity from its current value to 10. This process effectively dims the glow effect, giving the appearance of turning it off.

IEnumerator FadeEmission(float startIntensity, float endIntensity, float duration)

private IEnumerator FadeEmission(float startIntensity, float endIntensity, float duration)
{
    float elapsedTime = 0f;
    while (elapsedTime < duration)
    {
        float intensity = Mathf.Lerp(startIntensity, endIntensity, elapsedTime / duration);
        glowMaterial.SetFloat("_Opacity", intensity);
        elapsedTime += Time.deltaTime;
        yield return null;
    }
    glowMaterial.SetFloat("_Opacity", endIntensity);
}

This coroutine handles the core functionality of fading the emission intensity of the material. It takes three parameters: startIntensity, endIntensity, and duration, representing the initial intensity, target intensity, and duration of the fade, respectively. Within a while loop, it interpolates between the start and end intensities over the specified duration using Mathf.Lerp(). During each iteration, it updates the _Opacity property of the glow material to reflect the interpolated intensity value. Upon reaching the end of the duration, it sets the emission intensity to the target value, completing the fade effect seamlessly.

Future Expansion

N/A

Full Code Reference

using System.Collections;
using UnityEngine;

public class GlowController : MonoBehaviour
{
    private Renderer objectRenderer;
    private Material glowMaterial;

    private void Start()
    {
        objectRenderer = GetComponent<Renderer>();

        if (objectRenderer != null)
        {
            glowMaterial = objectRenderer.material;
        }
        else
        {
            Debug.LogError("Renderer component not found!");
        }
    }

    public void TurnEmissionOn()
    {
        if (glowMaterial != null)
        {
            StartCoroutine(FadeEmission(glowMaterial.GetFloat("_Opacity"), 0f, 2f));
        }
    }

    public void TurnEmissionOff()
    {
        if (glowMaterial != null)
        {
            StartCoroutine(FadeEmission(glowMaterial.GetFloat("_Opacity"), 10f, 4f)); 
        }
    }

    private IEnumerator FadeEmission(float startIntensity, float endIntensity, float duration)
    {
        float elapsedTime = 0f;
        while (elapsedTime < duration)
        {
            float intensity = Mathf.Lerp(startIntensity, endIntensity, elapsedTime / duration);
            glowMaterial.SetFloat("_Opacity", intensity);
            elapsedTime += Time.deltaTime;
            yield return null;
        }
        glowMaterial.SetFloat("_Opacity", endIntensity);
    }
}
⚠️ **GitHub.com Fallback** ⚠️