Game Development Reference
In-Depth Information
36 //Stop all running coroutines
37 StopAllCoroutines();
38
39 //Start new sequence
40 StartCoroutine(PlayLerpColors());
41 }
42 //------------------------------------------------
43 //Start animation
44 private IEnumerator PlayLerpColors()
45 {
46 //Lerp colors
47 yield return StartCoroutine(LerpColor(Source, Dest));
48 yield return StartCoroutine(LerpColor(Dest, Source));
49 }
50 //------------------------------------------------
51 //Function to lerp over time, from Color X to Color Y
52 private IEnumerator LerpColor(Color X, Color Y)
53 {
54 //Maintain elapsed time
55 float ElapsedTime = 0.0f;
56
57 //Loop for transition time
58 while(ElapsedTime <= TransitionTime)
59 {
60 //Update Elapsed time
61 ElapsedTime += Time.deltaTime;
62
63 //Set sprite renderer colors
64 foreach(SpriteRenderer SR in SpriteRenderers)
65 SR.color = Color.Lerp(X, Y, Mathf.Clamp(ElapsedTime/TransitionTime,
0.0f, 1.0f));
66
67 //Wait until next frame
68 yield return null;
69 }
70
71 //Set dest color
72 foreach(SpriteRenderer SR in SpriteRenderers)
73 SR.color = Y;
74 }
75 //------------------------------------------------
76 } //Send enemy destroyed notification
Note In Listing 7-3, coroutines have been used to create a ping-pong effect between color data structures.
There are, however, other methods for achieving similar ping-ponging behavior. For example, see the online
Unity documentation at https://docs.unity3d.com/Documentation/ScriptReference/
Mathf.PingPong.html .
 
Search WWH ::




Custom Search