Search Unity

2D Character Movements

Discussion in '2D' started by SolarPetal, Dec 24, 2015.

  1. SolarPetal

    SolarPetal

    Joined:
    Nov 7, 2015
    Posts:
    11
    Hi!

    I'm very new to the Unity engine (and to programming as well), I did a few basic projects (Text based adventure game and a basic block breaker with particles and sound). I have basic knowledge of programming, but lately I've been trying to code a "controller" for my 2 character many different ways and I can't find the right one.
    (I have animations done that I linked with the character's movements)

    First I tried moving it with the arrows, using two floats for the Horizontal and Vertical axis (like this) :
    Code (CSharp):
    1.  
    2. publicAnimator anim;
    3. publicRigidbody2D rbody;
    4. private float inputH;
    5. private float inputV;
    6.  
    7. anim = GetComponent<Animator>();
    8. rbody = GetComponent<Rigidbody2D>();
    9.  
    10. inputH = Input.GetAxis("Horizontal");
    11. inputV = Input.GetAxis("Vertical");
    12.        
    13.         anim.SetFloat("inputH", inputH);
    14.         anim.SetFloat("inputV", inputV);
    15.  
    16. float moveX = inputH*230f*Time.deltaTime;
    17. float moveY = inputV*230f*Time.deltaTime;
    But it didn't work out as I wanted and my character was like "sliding" on ice when moving and didn't stop right when I stopped pressing the key.

    I am right now trying my way with some very ugly, messy and heavy code but it's the only way that comes to my mind, I haven't been able to find a good tutorial online on 2D Sprite movement.
    Code (CSharp):
    1. public Animator anim;
    2. public Rigidbody2D rbody;
    3.  
    4.     void Start ()
    5. {
    6.     anim = GetComponent<Animator>();
    7.     rbody = GetComponent<Rigidbody2D>();
    8. }
    9. if (Input.GetKeyDown("d"))
    10.         {
    11.             anim.Play("PlayerRun(R)", -1);
    12.             rbody.velocity = new Vector2(1.6f, 0f);
    13.         }
    14.         if (Input.GetKeyUp("d"))
    15.         {
    16.             anim.Play("PlayerIdle(R)", -1);
    17.             rbody.velocity = new Vector2(0f, 0f);
    18.         }
    19.        
    20.         //RUN LEFT
    21.        
    22.         if (Input.GetKeyDown("q"))
    23.         {
    24.             anim.Play("PlayerRun(L)", -1);
    25.             rbody.velocity = new Vector2(-1.6f, 0f);
    26.         }
    27.         if (Input.GetKeyUp("q"))
    28.         {
    29.             anim.Play("PlayerIdle(L)", -1);
    30.             rbody.velocity = new Vector2(0f, 0f);
    31.         }      
    32.        
    33.         //JUMP
    34.        
    35.         if (Input.GetKey(KeyCode.Space))
    36.         {
    37.             anim.Play("PlayerJump(R)", -1);
    38.             //int jumP = rbody.velocity = new Vector3(0f, 5f, 0f);
    39.         }
    40.        
    41.         //ATTACKS
    42.        
    43.         if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("PlayerIdle(R)") && Input.GetKeyDown("k"))
    44.         {
    45.             anim.Play("PlayerSlash1(R)", -1);
    46.         }
    47.        
    48.         if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("PlayerSlash1(R)") && Input.GetKeyUp("k"))
    49.         {
    50.             anim.Play("PlayerIdle(R)", -1);
    51.         }
    52.        
    53.         if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("PlayerIdle(R)") && Input.GetKeyDown("l"))
    54.         {
    55.             anim.Play("PlayerSlash2(R)", -1);
    56.         }
    57.        
    58.         if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("PlayerSlash2(R)") && Input.GetKeyUp("l"))
    59.         {
    60.             anim.Play("PlayerIdle(R)", -1);
    61.         }
    62.        
    Right now, everything works fine, my character doesn't slide and it does stop moving when I release the "D" key, although I know what I'm writing is terrible and will break at some point.
    I would really appreciate if anyone would help me out there an explain and show precisely how I should code any 2D Character controller.

    Thanks !

    PS: (Here's a video of how it works as for now :
    )
    I do not claim the property of the sprites, they're property of GregarLink10 (http://gregarlink10.deviantart.com/)