How to play an AnimationClip when key is pressed in Unity3d, C#?
Solution 1
By the looks of your code you're never actually referencing the attached Animaton on your component. Try assigning anim it's component in the Start method, like this:
public class PlayAnimation : MonoBehaviour
{
public AnimationClip walk;
Animation anim;
void Start()
{
anim = GetComponent<Animation>();
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.W))
{
anim.clip = walk;
anim.Play();
}
}
}
Solution 2
The anim
variable is not initialized. You can initialize it in two ways:
1.GetComponent
If the Animation
component is attached to the-same Gameobject your PlayAnimation
script is attached to:
void Start()
{
anim = GetComponent<Animation>();
}
If the Animation
component is attached to different Gameobject:
void Start()
{
anim = GameObject.Find("GameObjectAnimationIsAttachedTo").GetComponent<Animation>();
}
2.Make anim
variable public
then assign it from the Editor.
Animation anim;
should be public Animation anim;
. Now, drag the GameObject with the Animation
component to the anim
variable.
Solution 3
This is my code now and it works, thanks to Frederik. I got now how the parameters work and the animator.
using UnityEngine;
using System.Collections;
public class PlayAnimation : MonoBehaviour {
public Animator animator;
byte idle=0;
byte walk=1;
byte sprint=2;
// Use this for initialization
void Start () {
animator = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.W)) {
animator.SetInteger ("Anumber", walk);
} else if (Input.GetKey (KeyCode.A)) {
animator.SetInteger ("Anumber", walk);
} else if (Input.GetKey (KeyCode.S)) {
animator.SetInteger ("Anumber", walk);
} else if (Input.GetKey (KeyCode.D)) {
animator.SetInteger ("Anumber", walk);
} else {
animator.SetInteger ("Anumber", idle);
}
}
}
Comments
-
Je moder about 3 years
- I have tried this code, but it doesn't work.
Can someone help me, and can someone explain things about animations in Unity 3D? I have tried so much, google, youtube etc.
using UnityEngine; using System.Collections; public class PlayAnimation : MonoBehaviour { public AnimationClip walk; Animation anim; void Update () { if (Input.GetKeyDown(KeyCode.W)){ anim.clip = walk; anim.Play(); } } }
-
Fredrik Schön about 7 yearsPhoto finish on that one, but you beat me to it! Ha!
-
Programmer about 7 yearsLol I know. I was typing when I saw your comment so I knew you were going to post. Anyways, +1.
-
Je moder about 7 yearsThanks for your reply, I have changed it in my script, but it doesn't work. I've got a 3d model with an animator, animation and my script in the inspector. But if I enable the animator, the animation plays all the time and if I release the W button too. If I disable him, there plays nothing.
-
Fredrik Schön about 7 yearsMay I suggest you check a quick tutorial on Animator, Animatons and how to control them? It's only 8 minutes: youtube.com/watch?v=JeZkctmoBPw and if you've got a somewhat grasp on it you can always watch it on higher speed! How I usually do things is to set up a pattern in the animator, then add variables to the transitions between animation, then control those variables with
animator.setBool("Walk", true);
for instance, thus telling the animator to animate the Walking animation -
Je moder about 7 yearsOkay, I will check it.
-
Je moder about 7 yearsAnd sorry for my bad English by the way, I am from the Netherlands.
-
Fredrik Schön about 7 yearsniet een probleem. sorry voor bad nederlands!
-
Je moder about 7 yearsI have seen the video, but I don't understand why parameters are useful. Can you explain me that better than the video.
-
Fredrik Schön about 7 yearsOh, ummm... Let's imagine we have a game where the player has 3 animations: Idle, Walk & Run. You can set a parameter float "movementspeed" which will be updated with the speed of the player every Update. In our animator we have
Idle -> Walk -> Run
where the->
represent a transition based on the movementspeed parameter. When Movementspeed is 0 we show idle, when it's 0><4 we show walk and when it's >5 we transition to Run. You can also control specific transitions between animation when you do it like this, for instance maybe the transition from Walk -> Run should start 50% in on Walk. -
Fredrik Schön about 7 yearsBy using parameters we get a dynamic lifecycle of animations. Animation.Play(); (I've never used it but I'm guessing:) just starts the animation on loop? Perhaps not a good example, I don't know. It's the only way I know of doing it, haha.
-
Je moder about 7 yearsHow can I do that, that a parameter changes when the speed changes of my character?
-
Fredrik Schön about 7 yearsAssuming you have a 2D game and an animator set up with a float parameter called "Movementspeed": in your Update() method you do
animator.SetFloat("Movementspeed", rigidbody.velocity.x);
where animator is an Animator (animator = GetComponent<Animator>();
) and rigidbody.velocity.x is the speed of the rigidbody in x-axis. -
Fredrik Schön about 7 yearsBut the key element here is to do animator.SetFloat("ParamterName", value); every frame so your animator knows the speed of every frame and can choose which animation to play based on the transition conditions.
-
Je moder about 7 yearsI did it, thanks for help. Great and in the Netherlands we say: Top!
-
Alox about 7 years@DaniëlvanderZwan Did this answer solve your question? If it did, be sure to accept it.
-
Fredrik Schön about 7 yearsIt seems @DaniëlvanderZwan replied with an answer that shows how he solved it, which is great! You should Accept that answer as the answer for people who needs similar help!