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 Uriel_96 · Apr 30, 2011 at 03:10 AM · animationjavascriptplaygetkeydown

problem not playing animation on keyDown

ROOKIE QUESTION. I am trying to make an animation to work when you press a key down but I can only get it work with en key pressed. This is simply what I have

if(Input.GetKeyDown("6")){
animation["myanimation"].speed = speed/10;
animation.Play("myanimation");
}

I already know a way to make it work that is like this:

var myanimation = false;
function Update(){
if(Input.GetKeyDown("6")){
myanimation = true;
}
if(myanimation == true){
animation["myanimation"].speed = speed/10;
animation.Play("myanimation");
if(/*myanimation finish*/){
myanimation = false;
}
}
}

the thing is that I want to know if there is a simple way to do this, so my question is how can I make a complete animation pressing down a key?

Comment
Add comment
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

2 Replies

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

Answer by DaveA · Apr 30, 2011 at 06:22 AM

It looks like that first part should just work. If you need to know if the animation is still playing, check animation.IsPlaying http://unity3d.com/support/documentation/ScriptReference/Animation.IsPlaying.html

Comment
Add comment · Show 1 · 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 Uriel_96 · May 02, 2011 at 12:01 AM 0
Share

thanks it actually works

avatar image
0

Answer by john 9 · May 12, 2011 at 02:02 AM

This is what i wrote for the same problem

function Awake() { animation["Idle"].layer = 0; animation["Run"].layer = 1; animation["Jump"].layer = 2; animation["Dead"].layer = 3; }

function Update() { if (Input.GetKey("d")) { animation["Run"].wrapMode = WrapMode.Loop; animation.Play("Run");

} else{ if (Input.GetKeyUp("d")) animation.Stop(); animation["Idle"].wrapMode = WrapMode.Loop; }

if (Input.GetKey("a")){ animation["Run"].wrapMode = WrapMode.Loop; animation.Play ("Run"); } else{ if (Input.GetKeyUp("a")) animation.Stop(); animation.CrossFade ("Idle");

}

if (Input.GetKey("right")) { animation["Run"].wrapMode = WrapMode.Loop; animation.Play("Run");

} else{ if (Input.GetKeyUp("right")) animation.Stop(); animation["Idle"].wrapMode = WrapMode.Loop; }

if (Input.GetKey("left")){ animation["Run"].wrapMode = WrapMode.Loop; animation.Play ("Run"); } else{ if (Input.GetKeyUp("left")) animation.Stop(); animation.CrossFade ("Idle");

}

if (Input.GetButtonDown("Jump")) { animation.CrossFade("Jump"); } else{ animation.CrossFade("Idle"); }

} Don't know if this will help?

Comment
Add comment · Show 1 · 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 john-essy · Jun 20, 2012 at 10:34 PM 0
Share

You just need one else to check if nothing ELSE is happening play idle ins$$anonymous$$d of trying to play idle when the key is released

if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) { animation.CrossFade("Walk"); } else { animation.CrossFade("Idle"); }

This stops all of this if the user is not pressing this button then play idle on each check. Do it this way and you only need to write it once. :)

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

No one has followed this question yet.

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Run Sprint animation and stop it when button is released 1 Answer

Playing animation when GameObject is destroyed 2 Answers

Unity model scripting question 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