Animating HUD element difficult puzzle!
I have tried to figure this out for a day: https://www.youtube.com/watch?v=_O4YLH5_cJY
Here is the relevant code in order of execution, I have put in if statements everywhere hoping to not have the animations conflict with each other. The script is run from the player by the way, not from each individual diamond. The one diamond collected is a public bool. I tried making it static so it's used by all scripts but that didn't seem to fix anything. There is clearly some conflict going on but I can't see what it is. Anyone who sees what the problem is?
void OnTriggerEnter(Collider Triggering)
{
if (Triggering.gameObject.tag == "Diamond")
{
Diamond.Play();
diamondsCollected += 1;
DiamondCollectTime();
}
}
void DiamondCollectTime()
{
if(oneDiamondCollected == false)
{
TextAnimation.GetComponent<Animation>()["DiamondCountAnimation"].speed = 1;
TextAnimation.GetComponent<Animation>().Play("DiamondCountAnimation");
oneDiamondCollected = true;
}
else if(oneDiamondCollected == true)
{
}
if (oneDiamondCollected == true)
{
StartCoroutine("CollectionDisappear");
}
IEnumerator CollectionDisappear()
{
if(oneDiamondCollected == true)
{
yield return new WaitForSeconds(3);
TextAnimation.GetComponent<Animation>()["DiamondCountAnimation"].speed = -1;
TextAnimation.GetComponent<Animation>().Play("DiamondCountAnimation");
oneDiamondCollected = false;
}
else if (oneDiamondCollected == false)
{
yield break;
}
}
Answer by Buffering13 · Mar 19, 2018 at 07:14 AM
Hey, it's Will. You linked this post on my youtube video (http://bit.ly/2G2yxLc) and I figured it would be easier to reply here.
Honestly I'm not entirely sure whats going on here. I would probably need to see more of your code to know what all is going on.
If I had to take a stab at it, based on this code and your video, You might have some weirdness going on with overlapping co-routines.
The best way I've found to troubleshoot these kinds of issues in Unity, is to use print statements to flag parts of the code. That way I can step through what exactly is happening, and see whats going on while the game is running. So I might put one print saying "Collided with diamond" "Started Disappear Co-Routine" etc, that you can see what is happening. You can delete them after you fix the issue, so your console isn't a mess.
Hope that was helpful!,Hey, it's Will. You linked this post on my UI youtube video and I figured it would be easier to post here.
Honestly I'm not exactly sure what's wrong here. I'm guessing I would need to see more of what's going on with the code to tell for sure.
If I had to take a stab at it, based on this code and the video, I'd say there's some weird stuff going on with overlapping co-routines. The best way I've found to pin point issues like this, is to put some print statements around in the code to flag when certain actions take place. Like "started disappear co-routine" "Collided with diamond" etc, at anywhere that makes sense that you can see what your code is doing in the console while it runs.
Hey thanks for bothering to reply, I figured it out since then.
You can't reverse animations that were created inside of Unity. So You have to create another animation in which the object goes in reverse.
The three seconds that it takes before the hud element slides out of view. That has to be made into an int and every time you collide with a new diamond that int has to be reset to 3 seconds. Somehow this has an effect on the Coroutine even though the coroutine isn't run over again.
I'm still having a small problem though, ever since I made the animation that animates the HUD element out of view the default state of the animated HUD is the first frame of the animation that animates the HUD out of view. I'm not using an animator so it doesn't seem to know which is the default state so it picks the last animation's first frame. How would you set the default state to something else without using an animator?
Your answer
Follow this Question
Related Questions
How to modify a value after the animation is done with it. 0 Answers
The weapon rotating slower than the torso of character when using IK to attach weapon to hands 0 Answers
How does one copy and paste keyframes in the Animation Editor? 4 Answers
BuildPipeline.BuildPlayer with AAB 0 Answers
Interpolated movement in a circle 1 Answer