[Absolute beginner] Play animation on trigger (trigger = several fallen objects) script
Hello everyone :)
So... I'm an absolute beginner, I don't understand what I find on the internet or I can't adapt it to my situation (or I didn't find it... then I'm very sorry for this post :/ (can you post a link, please ?)
What I want : I have cubes I shoot off platforms. When all the cubes of the scene are down on the floor, I want my animation (just a lift that goes up) to start playing.
I guess the idea is something like "If all components with cube material touch/have touched the floor material ==> playAnimation".
Does anybody know how to script that ? And where do I put that script (I think it's on the lift or on the animation, but I'm not sure) ?
Since I'm new to all of this, I unfortunately need a kind of step by step explanation. I know it's a lot to ask but that would help me greatly.
PS: I understand JS better, but a solution in c# is already perfect.
Thanks a lot :)
Answer by Ledifen · Aug 12, 2017 at 04:14 PM
In case someone needs this, here's the answer that works for me :
When you have animations in your game, there's a tab next to "Scene", "Game" and "Asset Store" named "Animator". Go there.
Once here, in the window, you must put an arrow (forgot the name) between "Any State" and your animation.
Then, next to the window, you're probably in the "Layers" panel so go to the "Parameters" panel
Click on the + and add a boolean (name it as you want).
Don't check the box (so the boolean = false)
Click on the arrow you created in step 2 and look at the inspector
Somewhere, there's a Conditions tab ==> chose your boolean and set the condition to "true"
Then, in the script attached to the trigger :
public int cubeToKnock = 3; // the number of objects (cube) that have to touch the trigger to play the animation
public Animator animatorLift; //The animator component that holds the animation I want to play
private int COF = 0; // the variable that counts the objects (cubes) that touch the trigger
void Start () {
animatorLift.enabled = true;
animatorLift.SetBool("levelFinished", false); //when you have animations in your game, there's a tab next to "Scene", "Game" and "Asset Store" named "Animator". There
}
void OnTriggerEnter (Collider Cube) {
if (Cube.gameObject.tag == "cube") {
COF ++; // each time an object with the cube tag touch the trigger, it adds 1 to COF
}
}
void AnimStart() {
animatorLift.SetBool("levelFinished", true); // a function that sets the value of the boolean you created in you animator window to true when it's called
}
void Update () {
if (COF == cubeToKnock) {
Invoke ("AnimStart", 1.5f); // call the function with a delay of 1 second and a half
}
}
}
There you go. Hope that helps some of you :)
If you have any suggestion to optimize the thing, fell free to share.
Your answer
Follow this Question
Related Questions
Jump animation not returning to normal? 0 Answers
How can I improve this code? 1 Answer
Simple 2D movement with animation while you are moving 0 Answers
Trigger the Animation #2 after Animation #1 was triggered 0 Answers
Help with a door slam trap 0 Answers