animating a Light2D via script
I have this script that generates a AnimationCurve assigns it to an AnimationClip, and then plays the clip. the code looks something like this: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Experimental.Rendering.Universal;
public class lighs : MonoBehaviour
{
private Animation anim;
private AnimationCurve curve;
private new Light2D light;
void Start()
{
anim = GetComponent<Animation>();
StartCoroutine(ShrinkCR(10));
light = GetComponent<Light2D>();
}
IEnumerator ShrinkCR(float time)
{
for (int i = 0; i < 3; i++)
{
yield return new WaitForSeconds(time);
yield return new WaitForSeconds(Shrink(anim));
}
}
private float Shrink(Animation anim)
{
AnimationClip clip = new AnimationClip
{
legacy = true
};
Keyframe[] keys;
keys = new Keyframe[2];
keys[0] = new Keyframe(0.0f, light.pointLightOuterRadius);
keys[1] = new Keyframe(1.0f, light.pointLightOuterRadius * 0.5f);
curve = new AnimationCurve(keys);
clip.SetCurve("", typeof(Light2D), "pointLightOuterRadius", curve);
keys = new Keyframe[2];
keys[0] = new Keyframe(0.0f, light.pointLightInnerRadius);
keys[1] = new Keyframe(1.0f, light.pointLightInnerRadius * 0.5f);
curve = new AnimationCurve(keys);
clip.SetCurve("", typeof(Light2D), "pointLightInnerRadius", curve);
anim.AddClip(clip, clip.name);
anim.Play(clip.name);
return clip.length;
}
}
but when I hit play mode, nothing happens. any idea why that's the case? if such functionality was just not added, any alternative to animate point light inner and outer radius?
I noticed that after 10 seconds the Animation component plays a clip animating 2 properties: "Light 2D.Point Light Inner Radius" and "Light 2D.Point Light Outer Radius" which is almost identical to what you would see if you were to animate the light normally from the editor except for a small "C# script" icon missing in the script animation. See screenshots attached
Your answer
Follow this Question
Related Questions
function behave unexpectedly inside a coroutine. 0 Answers
function behave unexpectedly inside a coroutine. 1 Answer
Having problems with making a Navmesh Agent stop and play an attack animation 0 Answers
How to play 'open door' animation on only one door? 0 Answers
Walk towards mouse cursor 0 Answers