• 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 Paul_BadBoy · Apr 06, 2014 at 01:06 PM · animationjavascriptmoving

Animation problem

Well, I got a little BIG problem. I want to play a animation while the W key is pressed down. It was working: the character was moving when pressing W, and the animation aswell. I have not changed anything in the scripts, but now the animation plays non-stop, and the character is not moving, also there is a big lag. I want the animation to play when any of W,A,S,D is pressed, If you can help me with that. Help please.

 function Start ()
 {
     if(Input.GetKeyDown("w"))
     {
         animation.Play("Walk", PlayMode.StopAll);
     }
 }
Comment
Add comment · Show 2
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 getyour411 · Apr 06, 2014 at 03:35 PM 0
Share

This code, since it's in Start() probably has nothing to do with your problem; show the code that's actually managing the move.

avatar image Paul_BadBoy · Apr 06, 2014 at 03:39 PM 0
Share

the code is the one that comes with the character controller asset

3 Replies

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

Answer by deltamish · Apr 06, 2014 at 01:27 PM

Hi,Why are you calling animation.Play in Start Start as the name suggests only gets called during begining of the level so use Update instead and Dont use GetKeyDown for input that changes constantly instead use GetKey

Try usinmg this snippet

 function Update()
 {
  if(Input.GetKey(KeyCode.W)) 
 {
 animation.Play("Walk", PlayMode.StopAll);
 }else if(Input.GetKey(KeyCode.A)) // Use KeyCodes much more efficient
 
 {
  animation.Play("WalkLeft", PlayMode.StopAll);
 }else if()
 {
  //similarly for all key presses
 }//finally after all your key press code
 
 else {
 animation.Play("Idle", PlayMode.StopAll);
 }
 
 
 
 }
Comment
Add comment · Show 8 · 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 Paul_BadBoy · Apr 06, 2014 at 01:49 PM 0
Share

yeah but I don't have any idle or walk left, can I use walk ins$$anonymous$$d?

avatar image tw1st3d · Apr 06, 2014 at 01:50 PM 0
Share

Converted answer by @Paul_BadBoy to comment. Please use the comment button when replying to an answer.

avatar image deltamish · Apr 06, 2014 at 02:09 PM 0
Share

sure @Paul_BadBoy i just wanted to show how it is done and if you use walk ins$$anonymous$$d of idle it will conitue to walk Hence just set its speed to 0; when in idle and back to its original speed when walking

 animation["Walk"].speed = 2f;
avatar image Paul_BadBoy · Apr 06, 2014 at 02:14 PM 0
Share

ok, if i made the idle animation that just stays in the same position, will it work, still my character cannot move, and I think the immense lag is created by the moving script

avatar image deltamish · Apr 06, 2014 at 07:37 PM 0
Share

yes by creating idle animation it will work as expectedd.

What lag the script only has a fwe else if cmd's are you sure you are not getting any error

Show more comments
avatar image
0

Answer by Master-of-Games · Apr 06, 2014 at 03:21 PM

use the animator.Its really easy to use.there some tutorial in youtube about the animator which can be really useful for your game.

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 Paul_BadBoy · Apr 06, 2014 at 03:37 PM 0
Share

thanks, now it is no more playing the walk animation

avatar image
0

Answer by iKatsuX3 · Apr 07, 2014 at 01:10 PM

Sample of my script... Use it... I already changed it to support yours try to improvise with it.

    if (Input.GetKeyDown(KeyCode.W));
    {
       Player.animation.CrossFade("Walk");
    }
    
    if (Input.GetKeyUp(KeyCode.W));
    {
       Player.animation.CrossFade("Idle");
    }

Side Notes:

Player = Character/Model

Idle,Walk = Animation

If your wondering where did I get this script... Try to watch Brackey's Tutorial.. :3

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 Paul_BadBoy · Apr 07, 2014 at 01:14 PM 0
Share

Well, I might test it if I can beat the lag and get the character to move. now my big problem is the movement, anyway, if the script that deltamish gave me doesn't work, I might give that a try

avatar image iKatsuX3 · Apr 08, 2014 at 03:51 AM 0
Share

Okay! Also, Do you use too many trees or grasses? If you do you should delete them first to $$anonymous$$imize the lag... Add them later on when your polishing your game! :3

Best Wishes On Your Game!.. :)

avatar image Paul_BadBoy · Apr 08, 2014 at 08:00 AM 0
Share

Well, I think I have better maps than BF4, a flat, empty world with grass texture on it :P

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

25 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mecanim not updating script when it switches state? 2 Answers

Attack Animation through Javascript 0 Answers

Playing a second idle animation... 1 Answer

How to change a variable in the same script? 3 Answers

Animated Object moving when clicked 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