Play animation with script
Spent 3 days of trying create simple animation of sphere... but no result. Please, said what's wrong with this script?
anim.Play doesn't work
but if I change it to anim.Stop -- it works.
using UnityEngine;
using System.Collections;
public class sphereScript : MonoBehaviour {
public Animator anim;
void Start() {
anim = GetComponent<Animator>();
anim.Stop();
}
void Update () {
anim.Play("sphereAnimation");
}
}
Animator.Play() function starts the animation. You are calling that every Update(). So the animation might start playing every frame, but it might not seem to play because it's starting again every frame. Try to call it once in Start() function and see what happens. Or you could put it inside if statement of pressing space for example:
void Update() {
if(Input.get$$anonymous$$eyUp($$anonymous$$eyCode.Space)) {
anim.Play("sphereAnimation");
}
}
documentation ca be found here: https://docs.unity3d.com/ScriptReference/Animation.Play.html
Answer by AurimasBlazulionis · Oct 26, 2016 at 03:55 PM
You have to create transitions. Most likely you will have to have a default animation without any motion applied. And a transition to an animation with a boolean or trigger as a parameter. Then you should do anim.SetBool("theParameter", true)
. A single answer can not explain everything, you should better watch this video made by Unity themselves.
thank you! I have already seen this video, but it's not easy to understand.
I have simple motion in my animation, it's only changing position of Sphere.
Is it a chance to Play this kind of animation without a transition? (like Adobe Flash do ;) )
You can use a legacy Animation component. That is simpler, or if it is even simpler you can just set position.
Answer by Tim-Duval · Nov 25, 2018 at 11:24 PM
if (Mathf.Abs(player.velocity.x) > 0 && onFloor==true)
{
anim.SetBool("Idle", false);
anim.SetBool("Walk", true);
}
else if(Mathf.Abs(player.velocity.x) == 0 && onFloor==true)
{
anim.SetBool("Walk", false);
anim.SetBool("Idle", true);
}
Ok, so I've tried this. I have all transition lines set to either true/false. Can you tell me what I'm doing wrong and why this isn't working?
Answer by rahulnaik9010 · Aug 28, 2020 at 02:26 PM
Don't put anim.Play("sphereAnimation"); inside the Update() Method.This will call the play animation in every frame. Put anim.Play("sphereAnimation"); in a single call.
Your answer
Follow this Question
Related Questions
BCE0020 error with animation 1 Answer
Animator does not show up 0 Answers
Animation not consistent 0 Answers
Animations in a blend tree. 0 Answers
Help please, movement and animator issue 0 Answers