Changing a sprite with a script - Animated GameObject

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:

public class PlayerController : OWCharacterController {
...
    public Sprite[] idleSprites;
    public int playerDirection;
...
   private SpriteRenderer spriterender;
...
   void Start () {
        spriterender = GetComponent<SpriteRenderer>();
...
    void Update () {
        if (<movement input>) {
...
           playerDirection = base.PickDirection(inputDirection);
...
        } else {
            spriterender.sprite = idleSprites[playerDirection];
        }

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.

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.

1 Like

I will check that out and let you know if it helps. Thanks!

No problem. It sounds logical. I hope it works for you lol :slight_smile:

1 Like

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.

1 Like

Okay, this is really cool. Exactly what I am trying to do.

Cool, glad you found it.

Did you make that tutorial?

No :slight_smile: