- Home /
How can I use parameters value in the animation
Hi,
I am trying to create an animation for a shield charge - that will set the transparency to the charge level gradually. the charge level is obviously not constant. the ugly way as I see it is writing a lot of logic in Update, but I would really like to refrain that. I tried using parameters but I can only use them as conditions not during the animation itself. how should this be implemented?
I tried putting some part of the animation (the shield has a few parts) that is not parameterized into the Animation and the other into Update, but I figured they not both work, is that right?
Thanks, Roy.
Answer by SteelArrow21 · Apr 28, 2014 at 05:01 AM
In this case, you dont want to use animations. You want to lerp the alpha value like this:
//C#
using UnityEngine;
using System.Collections;
public class ShieldCharge : MonoBehaviour {
public float speed = 1.0f;
void Awake(){
GameObject.renderer.material.color.a = Mathf.Lerp(0.0, 1.0, Time.deltaTime * speed);
}
}
Make a C# script called "ShieldCharge" and put it in the shield. When the script is activated, it will "charge" the shield.
Thank you very much, Just another thing, lets say I want to all sorts of shield animations and not necessarily on activating it (lets say lerp the transparency according its power by lerping, lerping the color to show a hit animation etc) all together. So I will implement this in the Update function, but the logic and code can become very clumsy.
what is the best strategy for it? (can I use yield for this to make the code simpler? something else?)
Thanks again.
Please don't post comments as answers. I moved your post to a comment - where it belongs.
sorry, my bad. to the point, I wanted to update on what I ended up doing: for all the "logic driven" animation, I removed everything from Update and ins$$anonymous$$d I used coroutines where relevant. for example, In the DamageShield() function I did the following:
public int DamageShield(int damage)
{
StartCoroutine(UpdateShieldActivatedAnimation());
//DO STUFF HERE
}
private IEnumerator UpdateFullShieldActivatedAnimation()
{
for (float r = 1.0f; r > 0.0; r -= Time.deltaTime * FULL_CHARGE_SHIELD_ACTIVATED_SPEED)
{
shieldColor.r = r;
spriteRenderer.color = shieldColor;
yield return null;
}
for (float r = 0.0f; r < 1.0; r += Time.deltaTime * FULL_CHARGE_SHIELD_ACTIVATED_SPEED)
{
shieldColor.r = r;
spriteRenderer.color = shieldColor;
yield return null;
}
shieldColor.r = 1.0f;
fullShieldActivated = false;
}
I guess the UpdateFullShieldActivatedAnimation code can be refactored, but the basic idea is solid. Thanks for the help!
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
How to stop mesh disappearing during Animator state transition blend time? 1 Answer
How to synchronize animation in Unity4.x 0 Answers
How can I change my AnimatorState instantly? 1 Answer
how to find out how many seconds into and animation that is playing 2 Answers