- Home /
Moving Animations
I have a script that plays four different walking animations when the w,a,s,d keys are pressed. When I hold two keys at once, the animations stop playing. How can I resume the animation of the first key pressed when a second key is pressed using this script?
function Start () {
}
var FootStepSound : AudioClip;
var JumpSound : AudioClip;
var MoveAmount = 0.15;
var JumpAmount = 250;
function Update () {
if (Input.GetKey(KeyCode.W)) {
transform.Translate (Vector3.forward * MoveAmount);
audio.clip = FootStepSound;
audio.Play();
animation.Play("F1WalkForward", PlayMode.StopAll);
}
if (Input.GetKey(KeyCode.A)) {
transform.Translate (Vector3.left * MoveAmount);
audio.clip = FootStepSound;
audio.Play();
animation.Play("F1WalkLeft", PlayMode.StopAll);
}
if (Input.GetKey(KeyCode.S)) {
transform.Translate (Vector3.back * MoveAmount);
audio.clip = FootStepSound;
audio.Play();
animation.Play("F1WalkBack", PlayMode.StopAll);
}
if (Input.GetKey(KeyCode.D)) {
transform.Translate (Vector3.right * MoveAmount);
audio.clip = FootStepSound;
audio.Play();
animation.Play("F1WalkRight", PlayMode.StopAll);
}
}
In future try to include more detail in your question, and indent your code. I will try to answer as best I can, making some assumptions.
Answer by rhys_vdw · Sep 21, 2012 at 05:13 AM
You should be starting your animations on Input.GetKeyDown, and stopping them on Input.GetKeyUp.
Also if you want two animations to play simultaneously, you should put them on different layers. For instance it might make sense to have walk forwards/backwards animations on one layer (because they should never play simultaneously), and your strafe left/right on another layer. When you change animation you should only use the default PlayMode.StopLayer.
To set the layer of an animation, you must change the AnimationState of that AnimationClip.
The AnimationState is a set of data of an AnimtionClip specific to the GameObject that is playing it. The AnimationClip is shared between all Animation components that have it in their animations, but each one will have its own AnimationState object to represent its speed, layer, time and weight.
Additionally you might like to use animation weights to blend your strafe and forward/backwards animations.
You can access and adjust weight and layers like this:
var clip1 : String;
var clip2 : String;
var fadePeriod : float = 2.0;
function Awake() {
// The [] operator gives us access to a clip's AnimationState
// Make clip2 take precedence over clip1.
animation[clip1].layer = 0;
animation[clip2].layer = 1;
// Start with clip2 having zero weight, this will allow
// clips playing on lower layers to be seen.
animation[clip2].weight = 0;
// Start both clips.
animation.Play(clip1);
animation.Play(clip2);
}
function Update() {
// Ping pong clip2's weight between weight 0 (min) and 1 (max). This will crossfade
// the animation back and forth between clip1 and clip2.
animation[clip2].weight = Mathf.PingPong(Time.time / fadePeriod, 1.0);
}
Note that for the above example to work, both animations must be specified by name in the inspector and must be set to loop.
Look at this guide for more information on animation blending.
If you have seen some of the newer first person shooters, you will notice that the weapon moves when the camera rotates, the character jumps, etc.
I was looking to make my game different and have different animations play for each movement key. I realized that when two keys are pressed, Unity doesn't know which animation to play.
I wanted the player to move in two directions at once (ex. Northeast) while playing the animation of the first key pressed.
Is this possible?
Wow, I think you just solved my problem. I switched animation.Play to animation.Crossfade. It plays the animation of the second key pressed while traveling in both directions at once. I'll still look into layers. Thanks.
Yeah, you could achieve this by putting them all on different layers. The one with the highest layer will be the one you see. Animations playing on lower layers will be invisible, but will still be playing, they will be visible again when higher layers stop playing.
Ah, you asked a question and I updated my answer... But you deleted the comment. I guess you worked it out by yourself. You should read my example code because I show how to use weights too.
Also I noticed in your question history you've closed a lot of your questions after they've been answered. You should leave your questions open, because better answers may exist: either because Unity3d API changes in future, or just because other approaches are available. An answer that worked for you might not necessarily be the best for somebody else, and UnityAnswers is a reference for a large number of people.
Best of luck with your project!
I did work it out.
I used layers, but rather than blending animations, I stopped the animation on the less relevant layer while both keys were being pressed.
I think I'll open some of those questions then.
Thanks :)
Answer by Anusha · Sep 20, 2012 at 05:16 AM
if (Input.GetKey(KeyCode.W))
{
}
else if (Input.GetKey(KeyCode.A))
{
}
else if (Input.GetKey(KeyCode.S))
{
}
else if (Input.GetKey(KeyCode.D))
{
}
Now, how can I play multiple animations on the same key?
what do you mean by play multiple animations on the same key??? what do you want your charecter to do if user press A and D ?
Answer by Coreyf716 · Sep 26, 2012 at 10:37 PM
I need help with something else. While working, I noticed a problem. When two keys that translate the player in opposite directions are pressed at the same time, the player does not move. If one is already being held and the other is pressed, the player stops.
How can I get the player to continue in the same direction the player was originally moving before the second key is pressed?
Post this question with your code in a new question. I'd have to see your code to show you how to adjust it.
Just add a link to it here as a comment so I get an email update about it.
Your answer
Follow this Question
Related Questions
Player Transformation 2 Answers
Using animations in prefabs 1 Answer
How do I animate a 2D sprite? 1 Answer
Expecting EOF found else?? 1 Answer
Attack While Running 1 Answer