Search Unity

Changing a sprite with a script - Animated GameObject

Discussion in '2D' started by robert-sherwood, Jan 14, 2018.

  1. robert-sherwood

    robert-sherwood

    Joined:
    Jan 7, 2018
    Posts:
    14
    Hey everyone, this is a pretty common question, and I've read the solutions, but they don't seem to work for me. I am developing a top-down 2D RPG, and I'm having a really dumb issue.

    The player character can move in 8 directions, and has appropriate movement animations for each direction. My goal is for the model to remain facing in the direction it was moving once it is done moving.

    In my player controller, I have a public int called playerDirection. Every time the character moves, playerDirection is set to a value between 0 and 7 (clockwise from north) based on a big if/else that reads the input. The animations are based on this number, and are working perfectly (i.e. when you press the left key, playerDirection is set to 6, and the "WalkLeft" animation triggers. There's a playerIdle animation that does not have a sprite associated with it in the animation controller. The animations are working very well.

    The playerDirection variable stays set until you move again, so I have a line in the Update() method for the player controller that chooses the appropriate sprite from an index that I declared and manually set up in the inspector (i.e. drag and drop individual sprites from a sheet to the right spot.)

    Code extract below:
    Code (CSharp):
    1. public class PlayerController : OWCharacterController {
    2. ...
    3.     public Sprite[] idleSprites;
    4.     public int playerDirection;
    5. ...
    6.    private SpriteRenderer spriterender;
    7. ...
    8.    void Start () {
    9.         spriterender = GetComponent<SpriteRenderer>();
    10. ...
    11.     void Update () {
    12.         if (<movement input>) {
    13. ...
    14.            playerDirection = base.PickDirection(inputDirection);
    15. ...
    16.         } else {
    17.             spriterender.sprite = idleSprites[playerDirection];
    18.         }
    That last line should set the sprite to one of the 8 that I set up, based on the playerDirection variable. I can see, in the debugger, that the playerDirection is being set correctly, but the sprite is always set back to the SpriteRenderer default sprite for the player object.

    All I can think of is that the animation is somehow interfering with the setting of the sprite. Any ideas?

    Thanks, again and apologies for the long question - wanted to make sure that all the relevent information is included.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Interesting question -- what about adding these 8 idle animations to a grouped animation (1 dimensional blend tree?).
    You could use your direction parameter there, too, I think, and set the idle animation based on it (+ whatever value says it's not moving?)
    Something like:
    moving? --> Choose animation
    else --> Go to blend tree: based on direction, choose idle animation.
     
    robert-sherwood likes this.
  3. robert-sherwood

    robert-sherwood

    Joined:
    Jan 7, 2018
    Posts:
    14
    I will check that out and let you know if it helps. Thanks!
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem. It sounds logical. I hope it works for you lol :)
     
    robert-sherwood likes this.
  5. ryandotdee

    ryandotdee

    Joined:
    Feb 6, 2015
    Posts:
    15
    I second methos5k, I was having almost exactly the same issue a couple of weeks back, and have turned to blend trees, which are working great.
     
    robert-sherwood likes this.
  6. robert-sherwood

    robert-sherwood

    Joined:
    Jan 7, 2018
    Posts:
    14
    Okay, this is really cool. Exactly what I am trying to do.

     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, glad you found it.
     
  8. robert-sherwood

    robert-sherwood

    Joined:
    Jan 7, 2018
    Posts:
    14
    Did you make that tutorial?
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No :)