- Create
FadingInteractable.cs
- Make
FadingInteractable
inherit fromInteractable
- 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
totrue
inInteract()
- Implement
Update()
- Add a guard clause to
Update()
that returns if_isFading
isfalse
- 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
- Knowing what we do about scripting in Unity, all of our fading logic will have to live in
- 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
- We'll try to initialize
- Add a guard clause to
Awake
that setsthis.enabled
tofalse
and returns ifTryGetComponent(out Renderer _renderer)
isfalse
- 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 of3
- Declare a private
float
field named "_elapsed"- We'll use
duration
and_elapsed
to track the state of the fade over time
- We'll use
- In
Update()
, setcolor.a
toMathf.Lerp(1, 0, _elapsed / duration)
- Every frame, we'll use the dividend of
_elapsed
andduration
to interpolate between 1 and 0 - 1 being 100% opaque and 0 being completely transparent
- Every frame, we'll use the dividend of
- Reassign
color
to_renderer.material.color = color
- Increment
_elapsed
byTime.deltaTime
at the end ofUpdate()
- 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 thanduration
- Finally, we can short circuit the function once we've suprassed the configured duration
Last active
October 4, 2021 19:51
-
-
Save charlieamat/9efed8529ca78d34b74242e32cc1211f to your computer and use it in GitHub Desktop.
[ta-edu-course-survival-game] Chapter 2 — Coroutines (1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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