Search Unity

2d RPG style character movement + animation

Discussion in '2D' started by ThirdEyeCyclops, Mar 5, 2014.

  1. ThirdEyeCyclops

    ThirdEyeCyclops

    Joined:
    Mar 5, 2014
    Posts:
    7
    Hi everyone, and thanks for checking out my thread.

    I'm currently struggling to grasp the new sprite animation system in unity 4.3 and I failed to create a simple RPG style character movement. (Something like the Pokemon game)

    I set up the sprite sheet, sliced them up in unity and have different animation states ready for idle, walk down, walk up, and walk left. I was also able to transition between these states with ease, however I can't get the character to face the current direction. For example, if the character is walking up, when he stops he faces towards the camera (which is the default idle state) instead of facing the up direction.

    I thought I could get it to work by using SpriteRenderer.Sprite = FacingUpSprite; but for some reason the spriterenderer.sprite cannot be edited during game play. Is there any way around this?


    I don't know what I'm doing wrong , I just want a simple character controller to work like this one here
    http://www.screencast.com/users/uDESIGNme/folders/Jing/media/d683f0e5-d886-45f6-8efb-1a27a5e87846

    Any help would be much appreciated :)

    Thanks.
     
    Last edited: Mar 14, 2014
  2. SiegfriedCroes

    SiegfriedCroes

    Joined:
    Oct 19, 2013
    Posts:
    569
    I think the reason you can't edit the sprite during gameplay is because an animation is playing and this animation is already changing the sprite so if you try to change it in code the animation that is currently playing will just change it back ;) I'm working on a game like this at the moment (see my signature) and I made an idle animation (with only 1 frame) for each direction. So when the player stops walking I just switch to this animation and the character will be standing still (afterwards you can always add some actual animation to the idle animations, like blinking eyes, character looking around,...).

    I hope this helps :)
     
  3. ThirdEyeCyclops

    ThirdEyeCyclops

    Joined:
    Mar 5, 2014
    Posts:
    7
    Hi! Thanks for the reply :)

    I guess that makes sense as to why I can't change the sprite through code. I think your way of doing idle for each state might be the best choice. Unfortunately my hard drive died on me so I lost everything. When I get unity installed and running again I will try out your method. Thank you.

    Btw your game looks awesome, you should definitely continue!
     
  4. Saiony

    Saiony

    Joined:
    Dec 8, 2013
    Posts:
    3
    ThirdEyeCyclops, can you tell me how did you do the animation? I have different animation states for walk left, walk right, walk down, walk up and idle, but I can't make the animation happen when the character moves in a direction.
     
  5. ThirdEyeCyclops

    ThirdEyeCyclops

    Joined:
    Mar 5, 2014
    Posts:
    7

    Hey Sajony.

    I spent all of today trying to get this to work and I just got it working :) It was actually very simple, I was just over complicating things.

    first of all you should take a look at these forum threads as there is very valuable information there for what you're trying to achieve.
    Also there is a video tutorial here:
    https://www.youtube.com/watch?v=7URRg8J6mz8

    //////////////////

    Now this is what I did, and it seems to work very well at the moment (haven't done too much testing).

    I set up the animator with two states, Idle and Walk then simply used a boolean parameter to switch between the two states. This is shown in picture one:
    http://s29.postimg.org/gmswqzqwn/animator1.jpg

    Both states are made up of a blend tree inside. The blend tree simply uses a parameter called posXY to switch between the different animations. Both idle and walk blend tree uses this parameter which makes it easier showing the right idle position after you stop walking. Picture below will explain it better hopefully :)
    http://s29.postimg.org/kkkru533r/idle.jpg
    http://s29.postimg.org/839it8ik7/walk.jpg

    then with a simple switch case script I update the posXY parameter depending on if the user is pressing w,a,s,d. the script below is a little sloppy but it works :)


    Code (csharp):
    1.  
    2. function FixedUpdate ()
    3. {
    4.     Charactermove();   
    5. }
    6.  
    7. function Charactermove()
    8. {
    9.     var xaxis = Input.GetAxisRaw("Horizontal");
    10.     var yaxis = Input.GetAxisRaw("Vertical");
    11.    
    12.    
    13.     if(yaxis){
    14.         switch(yaxis){
    15.             case 1:
    16.                 anima.SetBool("moving",true);
    17.                 anima.SetFloat("posXY",1);
    18.                 break;
    19.             case -1:
    20.                 anima.SetBool("moving",true);
    21.                 anima.SetFloat("posXY",-1);
    22.                 break;
    23.         }
    24.     }else if(xaxis){
    25.         switch(xaxis){
    26.             case 1:
    27.                 anima.SetBool("moving",true);
    28.                 anima.SetFloat("posXY",0.5);
    29.                 gameObject.transform.localScale.x = -1;
    30.                 break;
    31.             case -1:
    32.                 anima.SetBool("moving",true);
    33.                 anima.SetFloat("posXY",0.5);
    34.                 gameObject.transform.localScale.x = 1;
    35.                 break;
    36.         }
    37.     }else if(xaxis || yaxis == 0){
    38.         anima.SetBool("moving",false);
    39.     }
    40.    
    41. }
    42.  
    By the way, the script above only rotates the character and doesn't actually move it. to move the sprite, you would simply need to update the position when the buttons are pressed. Also, note that this method only makes use of 3 directions: up, down and left. There is no right animation or idle state because I simply used the scale code to flip the character, making him appear to be facing right.

    hope this helps!
    good luck with your developments!
     
    Last edited: Mar 14, 2014