- Home /
Animations not playing,Animations not working
i am making an FPS game, with the help of a tutorial on youtube i got the code below to make my player have walk / idle animations. they just dont play. no errors in the console.
using UnityEngine; using System.Collections;
public class animManager : MonoBehaviour {
public Animation am;
public AnimationClip walk;
public AnimationClip idle;
public Rigidbody rb;
void Update()
{
if (rb.velocity.magnitude >= 0.1)
{
playAnim(walk.name);
}
else
{
playAnim(idle.name);
}
}
public void playAnim(string animName)
{
am.CrossFade(animName);
}
public void stopAnim()
{
am.Stop();
}
}
Answer by shadowpuppet · Apr 15, 2018 at 08:24 PM
I use unity 4.7 so i don't know...but is "playAnim" a new thing? I I don't see where the script recognizes what it means. If this script is on the gameObject with the animation I do like the sample below. As long as you have those animations listed in your Animation component the script will find them by name so no need to make each possible animation a public variable
Animation anim;
void Start(){
anim = GetComponent<Animation>();
}
//then to play a specific animation
anim.Play("walk");
//or
anim.CrossFade("walk");
// OR AS I WOULD PUT IT IN YOUR SCRIPT//////////__
Animation anim;
Rigidbody rb;
void Start(){
anim = getComponent<Animation>();
rb= getComponent<RigidBody>();
anim.Play("idle");
}
void Update()
{
if (rb.velocity.magnitude >= 0.1)
{
anim.Crossfade("walk");
}
else
{
anim.Crossfade("idle');
}
}
// no need to actually stop animation - it will look like the game froze, just transition to an idle
Answer by TheDutch3vil · Apr 16, 2018 at 10:29 AM
well that seemed to work. thank u
cool, glad I could help. Could you please mark the answer as accepted? It helps when people are looking for answers to similar situation and see that it has a solution. Also for people ( like me) that search through the questions to try to help people who have not gotten a satisfactory answer yet
Your answer
![](https://koobas.hobune.stream/wayback/20220612160734im_/https://answers.unity.com/themes/thub/images/avi.jpg)