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 westlukasz · Jun 08, 2015 at 05:35 AM · animationunity 5javascriptkey

Animation Played when key pressed Help

I want to make a horse play it's idle animation when no key is pressed, it's walking animation when W, A, S, or D is pressed, and its running animation when the left shift is pressed. I just want to know how my script should be changed in order for this to happen.

pragma strict

 public var Run : AnimationClip;
 public var Walk : AnimationClip;
 public var Idle : AnimationClip;
 
 
 
 function Update () {
 
 GetComponent.<>(Idle).Play();
 
  if( Input.GetKeyDown("W")){
      
 GetComponent.<Animation>().Play();
  
  }
   if( Input.GetKeyDown("A")){
  GetComponent.<Animation>().Play();
      
  }
   if( Input.GetKeyDown("S")){
      GetComponent.<Animation>().Play();
      
  }
   if( Input.GetKeyDown("D")){
       GetComponent.<Animation>().Play();
      
  }
   if( Input.GetKeyDown("LeftShift")){
       GetComponent.<Animation>().Play();
      
     }
  
 }



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 crohr · Jun 08, 2015 at 07:16 AM

First off, I suggest caching the reference to the Animation component in Start. But to play the idle animation simply call animation.Play("idle"). So long as the Animation contains a clip with the name "idle" the animation with play.

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 westlukasz · Jun 09, 2015 at 12:07 AM 0
Share

I tried animation.PLay but it says that is is obsolete. It makes me use GetComponent.().Play("Idle"); ins$$anonymous$$d. But that doesnt work either. It also wont let me add in the Run, Walk, and Idle animations in the inspector

avatar image crohr · Jun 12, 2015 at 04:36 AM 0
Share

Does the animation you are trying to call contain all the animations? Because if you use did GetComponent().Play("idle") should work.

avatar image westlukasz · Jun 13, 2015 at 03:04 AM 0
Share

It doesnt, I am trying to call 3 separate animations. I tried to put all 3 animations in an animator but I cant add the animator in the by removing the three vars and adding a public animator var (public car animator : Animator;).

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help with attacking, and death animation. Unity 5 javascript 1 Answer

How to loop unity?JS 3 Answers

How to access Animation using java script & attach it to the object ? 2 Answers

Horizontal axis not working after animation 1 Answer

Animation depending on the Int value 0 Answers


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