Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Coreyf716 · Sep 19, 2012 at 10:31 PM · animationjavascriptkeycode

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);
 
 }
 
 }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rhys_vdw · Sep 21, 2012 at 04:52 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Coreyf716 · Sep 21, 2012 at 10:38 AM 0
Share

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?

avatar image Coreyf716 · Sep 21, 2012 at 10:46 AM 0
Share

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.

avatar image rhys_vdw · Sep 23, 2012 at 01:46 AM 0
Share

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.

avatar image rhys_vdw · Sep 25, 2012 at 11:43 PM 0
Share

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!

avatar image Coreyf716 · Sep 26, 2012 at 10:31 PM 0
Share

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 :)

avatar image
1

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))
  {
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Coreyf716 · Sep 20, 2012 at 10:20 PM 0
Share

Now, how can I play multiple animations on the same key?

avatar image Anusha · Sep 21, 2012 at 04:10 AM 0
Share

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 ?

avatar image Coreyf716 · Sep 26, 2012 at 10:28 PM 0
Share

Never $$anonymous$$d.

avatar image
0

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rhys_vdw · Sep 26, 2012 at 11:11 PM 0
Share

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.

avatar image Coreyf716 · Sep 26, 2012 at 11:27 PM 0
Share

http://answers.unity3d.com/questions/323654/player-transformation.html

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges