- Home /
A simple animation clip scripting control example... DOES NOT EXIST! - loosing clumps of hair on this one
OMG! This is SOOOO entirely frustrating! The documentation does not provide a cohesive overview of what where wyhy and how to script animations or clips or whichever... I still dont know the difference between an animation and an animation clip, aside from that clips go in a animation. Okay they go in an animation component on the object we want to animate, go that far. NOW, do I put a separate C# script on every single object i want to animate? if no then where does it go? Then once we have a home for the script(S), how in the {bleep} do we implement simple play and stop then next play, etc? Ho do I communicate to the multiple objects I want to animate with just simple controls?
Dont wanna blend or fade or .... just need a simple example!
I dont have time to dig into a character animation resource example to try to reverse engineer something that should be stupid simple
Answer by ByteSheep · Oct 12, 2012 at 06:34 AM
Make sure the animation clip you want to play is added in the animation component.
Then add the script to the object with the animation component (you could also put it on another object and reference the object with the animation you want to play).
To tell the animation component to play the default clip use this:
animation.Play();
Or to call a specific animation clip:
animation.Play("ClipName");
Documentation: http://docs.unity3d.com/Documentation/ScriptReference/Animation.Play.html
Here's an example in c# of calling an animation when a button is pressed:
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Update() {
if (Input.GetButtonUp("Fire1")) {
animation.Play();
}
}
}
Okay, after pulling the last bits of hair out, I finally got it to work . Similar to your example.
using UnityEngine; using System.Collections;
public class example : $$anonymous$$onoBehaviour { void Start() { animation.Play(); } }
So, to keep from having a bunch or individual scripts on multiple objects which may have multiple animations, how can these animation & clips be referenced from other scripts? Like if i created an animation controller or a sequence manager....
PS from the vantage point of developer new to Unity and C#, the documentation even the referenced page is entirely too vague. The reference manual totally skips over what needs to happen to get to the topics it does cover in some detail.
If you are creating controls for one main character object (where you want to play certain animations depending on user input) then I would recommend using one script for the character and using a separate script on each of the other objects to control their animations.
The way you would control the main characters animations through users input (e.g. when player presses left mouse button -> shoot) would be to use:
if (Input.GetButtonUp("Fire1")) {
animation.Play("shoot");
}
if (Input.GetButtonUp("Jump")) {
animation.Play("jump");
}
This code would play the "shoot" animation when the player presses the left mouse button(Fire1) and the "jump" animation when the player presses the space bar.
This is how you can play various animations of a certain object through user input.
However, just in case you don't want to play animations depending on user input and you for example want them to play after a certain time or so, then you can access another objects animation script by using:
GameObject.Find("*TheGameObjectsName*").animation.Play();
This will find the object with the name that you specify and will play it's animation. This is quite costly though as the Find() method isn't very sufficient - better if you can avoid using this method!
Link to Find() method: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html
Link to animation documentation: http://docs.unity3d.com/Documentation/ScriptReference/Animation.html
Link to detailed animation 'tutorial': http://docs.unity3d.com/Documentation/$$anonymous$$anual/AnimationScripting.html
Thanks $$anonymous$$X!
That was a huge help.... Hopefully some of may hair will grow back, lol. I truly appreciate your patience with your explanations. Even though you may have iterated what was already there, I think it was just a matter of getting a better grasp of the big picture on a more basic level...
Thanks!
Hey, I tried the codes and the animation works. But the animation restarts from the same place after pressing Space button or whatever is assigned ;i.e. The jump animation plays on the same place. I want the character to move forward but it returns to the same place.
its said that some of the children education books are made with unity.. How to provide such books with unity.. is there any relevant examples to follow??
Answer by ovirta · Feb 26, 2016 at 08:19 PM
Change your animation to Legacy as instructed here: http://answers.unity3d.com/questions/331268/a-simple-animation-clip-scripting-control-example.html
This is a real link to what this answer is instructing: https://answers.unity.com/questions/577801/animation-must-be-marked-as-legacy.html