• 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 Captrandom · Aug 24, 2015 at 01:14 PM · 2d game2d-physics2d animation

(2D) Movement-Animation Problems

I am pretty new to Unity and coding and I am following a tutorial, I have two different scripts, One for the animations and one for the movement it self. My problem is that when I change direction, the animation continues to loop and since I have no clue how to fix it I went on here to ask for help from the pro people.

Here is my movement script: using UnityEngine; using System.Collections;

 public class Player_Movement : MonoBehaviour {
 
     public float speed;
 
 
     void Update () {
 
         if(Input.GetKey(KeyCode.W)){
             GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 1) * (speed));
         }
 
         if(Input.GetKey(KeyCode.S)){
             GetComponent<Rigidbody2D>().AddForce(new Vector2(0 ,-1) * (speed));
         }
 
         if(Input.GetKey(KeyCode.D)){
             GetComponent<Rigidbody2D>().AddForce(new Vector2(1, 0) * (speed));
         }
 
         if(Input.GetKey(KeyCode.A)){
             GetComponent<Rigidbody2D>().AddForce(new Vector2(-1, 0) * (speed));
         }
 
     
     }
 }

And here is my animation script:

 using UnityEngine;
 using System.Collections;
 
 public class Player_Animations : MonoBehaviour {
 
     public Animator anim;
 
     void Start () {
         anim = GetComponent<Animator>();
     
     }
     
     void Update () {
 
         if(Input.GetKey (KeyCode.W)){
             anim.SetBool("Up",true);
             anim.SetBool("Down",false);
             anim.SetBool("Right",false);
             anim.SetBool("Left",false);
             }
 
         if(Input.GetKey (KeyCode.S)){
             anim.SetBool("Up",false);
             anim.SetBool("Down",true);
             anim.SetBool("Right",false);
             anim.SetBool("Left",false);
             }
 
         if(Input.GetKey (KeyCode.D)){
             anim.SetBool("Up",false);
             anim.SetBool("Down",false);
             anim.SetBool("Right",true);
             anim.SetBool("Left",false);
             }
 
         if(Input.GetKey (KeyCode.A)){
             anim.SetBool("Up",false);
             anim.SetBool("Down",false);
             anim.SetBool("Right",false);
             anim.SetBool("Left",true);
             }
 
             if(Input.GetKey(KeyCode.W)){
                 anim.SetBool("Walking_Up",true);
             }
             else{
                 anim.SetBool("Walking_Up",false);
             }
 
             if(Input.GetKey(KeyCode.S)){
                 anim.SetBool("Walking_Down",true);
             }
             else{
                 anim.SetBool("Walking_Down",false);
             }
 
             if(Input.GetKey(KeyCode.D)){
                 anim.SetBool("Walking_Right",true);
             }
             else{
                 anim.SetBool("Walking_Right",false);
             }
 
             if(Input.GetKey(KeyCode.A)){
                 anim.SetBool("Walking_Left",true);
             }
             else{
                 anim.SetBool("Walking_Left",false);
             }
 
 
 
 
     }
 }
 

As I said they are simple scripts and are probably insufficient but it is the best I can do at the moment.

Also, here is my Animator.

alt text

Following the tutorial I set up the Idles to eachother, for example Player_Idle_Down -(Right-true)- Player_Idle_Right. Then the Idles are connected to the correct Walking animation, for example Player_Idle_Left -(Walking_Left-true)- Player_Walking_Left and then back Player_Walking_Left -(Walking_Left-false)- Player_Idle_Left

Again, probably inadequate, but it is the only way I know how to do it.

I hope I've been informative enough so you understand my problem. Also please be gentle with the coding language as I am a noob xD

-Captain

animator.png (38.3 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

2D ledge grabbing -1 Answers

Unity 5.2.2 2D c# If lever hit then door open. 0 Answers

2D Dynamic Lights and Shadows 0 Answers

How can I create random butterfly flying over a random 2d path. 0 Answers

Best way to make a game object have a "springy" toggle button feel, similar to a ballpoint pin 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