Skip to content

Instantly share code, notes, and snippets.

@charlieamat
Last active October 4, 2021 19:51
Show Gist options
  • Save charlieamat/9efed8529ca78d34b74242e32cc1211f to your computer and use it in GitHub Desktop.
Save charlieamat/9efed8529ca78d34b74242e32cc1211f to your computer and use it in GitHub Desktop.
[ta-edu-course-survival-game] Chapter 2 — Coroutines (1)
  • Create FadingInteractable.cs
  • Make FadingInteractable inherit from Interactable
    • We're going to take advantage of our interaction system
    • When the Player interacts with the object, it'll fade out
  • Implement Interact()
  • Declare a private bool field named "_isFading"
    • We'll need to keep track of when the object is fading
  • Set _isFading to truein Interact()
  • Implement Update()
  • Add a guard clause to Update() that returns if _isFading is false
    • Knowing what we do about scripting in Unity, all of our fading logic will have to live in Update()
    • We can just short circuit the function when the object isn't fading
  • Declare a private Renderer field named _renderer
    • To fade the object, we're going to manipulate the alpha value of object's material, which is located in the object's renderer
    • So we're going to make the assumption that the object has a renderer attached to it
  • Implement Awake()
    • We'll try to initialize _renderer when the game starts
  • Add a guard clause to Awake that sets this.enabled to false and returns if TryGetComponent(out Renderer _renderer) is false
    • If it isn't present on the GameObject, we'll just disable it to avoid exceptions down the line
  • Declare a local variable to Update() named "color"
  • Initialize color using _renderer.material.color;
    • With access to the material, we can now begin updating the color property's alpha value
    • But if we go from 0 to 1 in a single frame, the fading effect won't be seen
  • Declare a private, serialized float field named "duration" with a default value of 3
  • Declare a private float field named "_elapsed"
    • We'll use duration and _elapsed to track the state of the fade over time
  • In Update(), set color.a to Mathf.Lerp(1, 0, _elapsed / duration)
    • Every frame, we'll use the dividend of _elapsed and duration to interpolate between 1 and 0
    • 1 being 100% opaque and 0 being completely transparent
  • Reassign color to _renderer.material.color = color
  • Increment _elapsed by Time.deltaTime at the end of Update()
    • We need to increment the elapsed time of the fade every frame to see progress
  • Add a guard clause to Update() that returns if _elapsed is greater than duration
    • Finally, we can short circuit the function once we've suprassed the configured duration
public class FadingInteractable : Interactable
{
[SerializeField] private float duration = 3;
private float _elapsed;
private Renderer _renderer;
private bool _isFading;
public void Interact()
{
_isFading = true;
}
private void Awake()
{
if (!TryGetComponent(out Renderer renderer))
{
this.enabled = false;
return;
}
}
private void Update()
{
if (_isFading) return;
if (_elapsed > duration) return;
var color = _renderer.material.color;
color.a = Mathf.Lerp(1, 0, _elapsed / duration);
_renderer.material.color = color;
_elapsed += Time.deltaTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment