Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Barnes12 · Jun 10, 2017 at 10:24 AM · animationunity 2d

how to make an animation play on keypress unity game maker

so what i want to do is when the key is not pressed i want my character to play the "idle" animation, but when I want my character to walk i want him to play the "sprite" animation, I am super new to unity and will appreciate to know what i can do to fix this

 public Animator anim;

 public KeyCode RightKey;

 // Use this for initialization
 void Start()
 {
     if (Input.GetKeyDown(RightKey))

         anim.Play("sprite");



     if (Input.GetKeyUp(RightKey))

         anim.SetTrigger("idle");

 }

 // Update is called once per frame
 void Update()
 {
    
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Aaranos · Jun 10, 2017 at 09:34 PM

You have to put your if statements in Update. The Start function is only ran at start so it wouldn't check for them later.

If you haven't already, you will also want to setup the animations in Animator. You would want your Idle to be the default. Your trigger would be assigned to the animation you want to trigger, and then it would go back to Idle. That eliminates the need to check for key up.

Also, you will have to assign your animator component.

A few reference pages that may help: https://docs.unity3d.com/ScriptReference/KeyCode.html https://docs.unity3d.com/Manual/class-Animator.html

  public Animator anim;
  // Use this for initialization
  void Start()
  {
      anim = gameObject.GetComponent<Animator>();
  }
  // Update is called once per frame
  void Update()
  {
     if (Input.GetKeyDown(KeyCode.RightArrow))
          anim.Play("sprite");
  }
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 Menzle · Apr 21, 2020 at 05:25 PM 0
Share

And can i do an else statement? like when I remove the Down and when I don't press the Button to it to stop

avatar image itsoutchy · Apr 10 at 10:46 PM 0
Share

doesn't work, as it shows the following error ![alt text][invalid layer index '-1']

[1]: /storage/temp/194801-error-animate.png

error-animate.png (6.7 kB)

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

7 People are following this question.

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

Related Questions

Unity Multi Application Problem 0 Answers

Apk on android does not start. Reason: Animator | Не запускается Apk на андройд. Причина: Animator 0 Answers

my animtion plays when its not ment to too 0 Answers

How do you stop a 2D character from changing it's X axis while performing an animation? 1 Answer

How do I play a certain animation on keydown? 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