hi , as topic says i have a series of gameobjects which are called Items.
but i need after Items’ animation clip end , the child of each one get appeared or be activated. this child is as a timer. i have already made timer as prefab. also i dont know how to instantiate this prefab for each Item rather than creating child for each Item gameobject . i would be happy to receive some ideas!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class controller : MonoBehaviour
{
public GameObject[] Items;
private List<Animator> animlist;
void Start ()
{
animlist = new List<Animator>();
for(int i=0; i<Items.Length; i++)
{
animlist.Add(Items[i].GetComponent<Animator>());
}
StartCoroutine (AnimationRandomPlaying());
}
IEnumerator AnimationRandomPlaying()
{
yield return new WaitForSeconds(2);
Items[0].GetComponent<Animator>().Play("item01");
Items[1].GetComponent<Animator>().Play("item02");
Items[2].GetComponent<Animator>().Play("item03");
Items[3].GetComponent<Animator>().Play("item04");
Items[4].GetComponent<Animator>().Play("item05");
Items[5].GetComponent<Animator>().Play("item06");
Items[6].GetComponent<Animator>().Play("item07");
Items[7].GetComponent<Animator>().Play("item08");
Items[8].GetComponent<Animator>().Play("item09");
}
}
Animation clips can have event functions placed on them in the unity inspector. Any scripts on the animated object with the same name as the function you set in the inspector will get called at that point in the animation.
So as an example you could have a function called AnimationFinished that then increments a counter or deactivates/destroys the game object etc., or all of the above. You could use this with any event manager you might use to inform other game objects that the animation is finished.
1 Like
Thanks. how those 9 Items animations can randomly run in group of 2 or 3 Items ?
P.S : i mean after 2 seconds that is there, how these Items in group of 2 or 3 can be moved?
then next 3 Items… then next 3 Items…
Lets say 3 Items,
no idea? i searched alot. i need a random distribution between an array of gameobject . i guess it would be like:
case group 01
random Items move…
case group 02
random Items move…
but i am new to programming, hence i like to request ideas from senior (senior < junior || junior < senior ??? hhhhh) programmers . thanls for any idea.
If I understand what you want, you want more than one item to run its animation at once, but not run the next multiples until all of the others have finished. I suppose what i would do is have 2 scripts, one would be the master script(has a list of the game objects needing animated), and second script that would be on each of those objects so they would have the function called from the animation event. this function would be pretty simple, it would just update the master script to tell it that its animation was finished.
In pseudo code it might look something like this :
// Master script pseudo code.
public class Master: MonoBehaviour
{
// A list of animators for game objects.
public List<Animator> animators;
// Tracks the number of animations that haven't finished.
public int numAnimationsPlaying { get; set; }
private int currentIndex = 0;
void Update()
{
// All animations have finished so start the next group.
if(numAnimationsPlaying == 0)
{
// pick a number of animations to wait on.
numAnimationsPlaying += 3;
for(int i = 0; i < numAnimationsPlaying; i++)
{
// Make sure we don't exceed the list size.
// We could wrap around to the beginning of
// the list if you wanted to.
if(currentIndex >= animators.Count)
{
currentIndex = 0;
break;
}
currentIndex += i;
animator[currentIndex].Play(animationName);
}
}
}
}
So this is pretty simple, you will notice that we only execute the code when numAnimationsPlaying has fallen to zero.
But we don’t actually decrement it anywhere in this script. Thats because we want the second script, the one that has the animation event function, to be the one decrement this variable after its animation has finished.
public class AnimEvent : MonoBehaviour
{
// Reference to the master script so we can use in from the animation function.
public Master masterScript;
// This function will be called by the unity animation after you make an animation event for it
// in the unity inspector.
void OnAnimationFinished()
{
masterScript.numAnimationsPlaying -= 1;
}
}
Anyway, there are a ton of ways you could go about something like this. But hopefully this helps.
1 Like
I guess i forgot the part about randomness. In the case of randomness i would probably have 2 lists of the animators, one for animators that haven’t played yet, and another for the ones that have. then every animator gets removed from one list and added to the other when you play the animation. Then you could just generate a random number in the range of 0 to list.Count - 1, to pick which animator to play.
1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class controller : MonoBehaviour
{
public GameObject[] Items;
public List<Animator> animlist;
public AnimEvent [] timesrun;
public Text Playerscores;
private float playerscores;
public Image PlayerLives;
private Text playerlives;
public int numAnimationsPlaying { get; set; }
private int currentIndex = 0;
void Update()
{
if(numAnimationsPlaying == 0)
{
numAnimationsPlaying += 3;
for(int i = 0; i < numAnimationsPlaying; i++)
{
if(currentIndex >= animlist.Count)
{
currentIndex = 0;
break;
}
currentIndex += i;
animlist[currentIndex].Play("anim01");
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AnimEvent : MonoBehaviour
{
public GameObject TimerCounter;
public controller masterScript;
public Image cooldown;
private bool coolingDown;
public float waitTime = 5.0f;
void Update()
{
if (coolingDown == true)
{
cooldown.fillAmount -= 1.0f / waitTime * Time.deltaTime;
}
if(cooldown.fillAmount == 0f)
{
}
}
public void Item01return()
{
masterScript.numAnimationsPlaying -= 1;
TimerCounter.SetActive (true);
coolingDown = true;
}
}
each item or gameobject in this code has 3 animations.
1.Idle
2. anim01
3.anim02
now when game runs , what happen is for each item animation Idle changes to anim01. and a timer counter appears.
here what exactly i need is if :
player click within timer is not ended , then item’s anim02 get played. and its timer disappears. and he , she get 1 score.
if player cannot within timer counts click on item , then he ,she gives away 1 lives , item anim02 get played. and timer disappers.
i am trying to understand it , but here actually is the most problematic part to code it.
i hope you take some times and help me .
I’m not at home to try the code but I think I get the concept better now. You want a random number of items to get selected from a list, and the player has to click them before a timer runs out. A very simple state machine would make something like this very easy, and you wouldn’t need a ton of ‘if’ statements to complicate the code. I would recommend getting very familiar with finite state machines(FSMs) as they make programming SO much easier.
In a nutshell, the button would need a few states scripted for it. It would only be in one state at a time.
Idle state : does nothing. Or updates a cooldown timer.
Active state: begins a timer. If clicked on during timer during this state, go to success state. If not, go to failed state.
SuccessState : update player score/lives. Return to idle state.
FailedState: decrease player lives. Begin cool down timer.
1 Like
so i will youtube about FSMs untill to understand it better , because i am new to programming. i hope until this question is solved you stay in help with me.
the problem now is that from 9 items, 5 of them take part in animation. lets make it so simple :
i have 9 item , each has 3 state animation : idle , run anim, return anim.
at the moment i need some code that , after game runs , after 2 seconds , randomly choose one of 9 items and run its run anim.
can you code that ? and then i follow your instrunctions step by step until this mechanism is done . and i learnt .
by the way above is a zip project which is exactly my project. its unity 5.6 version,
thanks.