- Home /
How do i change sprite regarding to position
So i have a basic top down space shooter type of game and i want to change the sprite of the rocket ship regarding to its x axis i have prepared this script but it doesnt work.
void SpriteChange()
{
if(position.x <= 2f)
{
this.GetComponent<SpriteRenderer>().sprite = RocketRight1;
}
if (position.x <= 2)
{
this.GetComponent<SpriteRenderer>().sprite = RocketRight2;
}
if (position.x >= -1)
{
this.GetComponent<SpriteRenderer>().sprite = RocketLeft1;
}
if (position.x >= -1)
{
this.GetComponent<SpriteRenderer>().sprite = RocketLeft2;
}
}
Please help im lost :/
Are you Sure your conditions becomes true? You are calling SpriteChange
anywhere i the code?
Answer by kalanadis · Jul 24, 2017 at 10:17 AM
it seems like the conditions are wrong.According to your logic,
only following statement will work
if (position.x >= -1) { this.GetComponent().sprite = RocketLeft2; }
Your answer
Follow this Question
Related Questions
My sprites wont change? 0 Answers
How to make walls that block light cones in 2D? 1 Answer
How do I make light not pass through 2D wall sprites? 1 Answer
Birds eye view 2D Animation using blend trees 0 Answers
Layer Sprites Based on Y axis 1 Answer