- Home /
Sprite Renderer Not Changing Sprite via Script
I am using the following function to change the sprite of player object during runtime using tiny RPG sprites. I have basically 3 separate idle sprites (idleRight, idleUp, idleDown) which are referenced in the editor. I want the player to be facing the last moved direction when it goes idle. These sprites will be programmatically changed via the swichIdleSprites() function. Here's the function.
public void switchIdleSprites(Vector2 lastMovement)
{
if (spriteRenderer != null && lastMovement != null)
{
spriteRenderer.flipX = false;
if (lastMovement.x > 0)
{
spriteRenderer.sprite = idleRight;
}
else if (lastMovement.x < 0)
{
spriteRenderer.sprite = idleRight;
spriteRenderer.flipX = true;
}
else if (lastMovement.y > 0)
{
spriteRenderer.sprite = idleUp;
}
else if (lastMovement.y < 0)
{
spriteRenderer.sprite = idleDown;
} else
{
spriteRenderer.sprite = idleRight;
}
}
}
This function will be called from the movement script when the player has stopped moving in a particular direction. Inside the update function of the movement controller, ..
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
if (movement.x != 0 || movement.y != 0) // If the player is moving
{
lastMovement = movement;
spriteHasChanged = false;
}
if (movement.x == 0 && movement.y == 0 && spriteHasChanged == false) // If the player has stopped but sprite has not changed
{
idleScript.switchIdleSprites(lastMovement);
spriteHasChanged = true;
}
}
I don't have a sprite facing left so I flipped the X in the sprite renderer. The problem is that sprites are not changing when I moved and stopped the player around. But when I checked the sprite renderer, flipX checked correctly when I idle from moving left and unchecked when I stopped in other direction. Its just that sprites are not changing.
No errors are shown in the console. I have tested with changing color on sprite renderer instead of sprite and it work correctly. Setting none to sprite via editor does not work too. I also checked that spirtes are referenced correctly in the unity editor and restarted the editor. I found that switchIdleSpirtes function is working as intended but its just that sprites are not changing programmatically. Is there anything I am doing wrong? please help me.
Answer by sztobar · Oct 03, 2021 at 12:16 PM
Hi, If that's all the code that is controlling the sprite I think that the second if condition in Update
method is incorrect. You are calling switchIdleSprites
only if movement.x == 0
and movement.y == 0
so basically only when the player is not moving. I would remove that condition and call switchIdleSprites
on every update.
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
if (movement.x != 0 || movement.y != 0) // If the player is moving
{
lastMovement = movement;
spriteHasChanged = false;
}
idleScript.switchIdleSprites(lastMovement);
spriteHasChanged = true;
}
I'm not sure what spriteHasChanged
variable is used for, but with change above sprite should be changed correctly.
Your answer
Follow this Question
Related Questions
My gameObject is invisible 0 Answers
Crop/Mask a Sprite on runtime 0 Answers
How to get the bounds of an actual sprite, not it's bounding box. 1 Answer
Unity2D Sprite Fading Issues 1 Answer