• 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 Midori48 · Feb 07, 2018 at 11:52 AM · 2d animation2d sprites2d controller

2D character movement

I'm sure I messed up, I apologize in advance for the trivial question: i've some problem with the animation of my character, and the respective script, i've set a boolean for play (the animation) with the "D" letter but the problem is that actually, if i press it again my character does not move a second time. the question is: using a Boolean in my code is actually the right choice? If it's not, and this is the trivial part, i should insert rb2d.AddForce? When i used my animation didn't play and my character does not move. Thanks in advance mates. alt text

alt text

screenshot-1.png (152.6 kB)
screenshot-2.png (121.6 kB)
Comment
Add comment · Show 1
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 PersianKiller · Feb 07, 2018 at 12:03 PM 0
Share

@$$anonymous$$idori48 ,try this

 public float moveSpeed;
 public Rigidbody2D rb;
 public Animator animator;
 // Use this for initialization
 void Start () {
     animator = GetComponent<Animator> ();
     rb = GetComponent<Rigidbody2D> ();
 }
 
 // Update is called once per frame
 void Update () {

     if(Input.GetAxisRaw("Horizontal")!=0){
         rb.velocity = new Vector2 (moveSpeed,rb.velocity.y);
         animator.Play ("RunAnimationName");
     }
     else if(Input.GetAxisRaw("Horizontal")==0){
         rb.velocity = new Vector2 (0,rb.velocity.y);
         animator.Play ("IdleAnimationName");
     }
     
 }
  }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by petarpejovic · Feb 07, 2018 at 02:11 PM

You can try with this :)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour {
 
     //[SerializeField]
     public float maxSpeed = 10f; // Maximum speed player can have
 
     //Facing of player. It can be Left or Right.
     bool facingRight = true; 
 
     // Rigidbody 2D component you need to add to Player GameObject
     private Rigidbody2D rb;
 
     private Animator animator;
 
 
     void Start ()
     {
         rb = GetComponent<Rigidbody2D>();
         animator.GetComponent<Animator>(); 
     }
     
     // It is better to do this things like movement in Fixed Update
     void FixedUpdate ()
     {
         ///<summary> You dont need if statement to check GetKeyDown(). GetAxix("Horizontal") will check if Player pressed keys (Left or Right Arrow, A or D).
         ///You can also change this in Input Settings if you want 
         ///</summary>
         ///
         float move = Input.GetAxis("Horizontal");
 
         rb.velocity = new Vector2(move * maxSpeed, rb.velocity.y);
         
         if(move > 0 && !facingRight) // Run Right
         {
             Flip();
             animator.Play("RunAnimationName");
         }
         else if (move<0 && facingRight) // Run Left
         {
             Flip();
             animator.Play("RunAnimationName");
         }
 
         else // Idle 
         {
             animator.Play("IdleAnimationName");
         }
     }
 /// <summary>
 /// This function Flip player Sprite Left or Right
 /// </summary>
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }

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 Midori48 · Feb 08, 2018 at 05:49 PM 0
Share

Thanks to you half of the problem is fixed (really, thanks for the help) but still, my character just move on the same spot, probably i should use something like transform.up(?).

avatar image Midori48 · Feb 08, 2018 at 05:54 PM 0
Share

No mate, i'm nuts, it works! :D (So damn happy and is just the beginning), thanks again!

avatar image PeriKuu · Jun 01, 2020 at 06:24 AM 0
Share

it says "No $$anonymous$$onoBehaviour scripts in the file, or their names do not match the file name"

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

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

Related Questions

How to stretch a sprite in 2d? 1 Answer

Help with dragonbones dynamic texture switching 0 Answers

How to make weapon match walking animation (2D)? 0 Answers

Why is my character changing position everytime it plays other animation aside from idle?,The position of my character lowers down even with collision 1 Answer

2D Animation works on preview but not on PLAY? 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