I'm trying to move a game object, pokemon, from the centre of the screen to the left of the screen (only along the horizontal axis), smoothly. However, it keeps teleporting instantly.
WHAT THE CODE DOES = Changes from one sprite to another sprite in an endless loop. When it changes to another sprite, it also changes its scale.
WHAT I WANT THE CODE TO DO = Before changing to a different sprite, move left on the horizontal axes.
I have tried linear interpolation (lerp) and transform.translate.
public class Transitions : MonoBehaviour
{
public Sprite Pokemon_0; // VARIABLES FOR SPRITES (Pokemons/ game objects)
public Sprite Pokemon_1;
float timer = 1f; // CHANGING SPRITES VALUES (delays)
float delay = 5f;
Vector2 temp; //CHANGING POSITION VARIABLES
Vector2 tempPos;
Vector2 finalPosition;
void Start()
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = Pokemon_0;
}
void Update()
{
timer -= Time.deltaTime;
if (timer <= 0)
{
if (this.gameObject.GetComponent<SpriteRenderer>().sprite == Pokemon_0) // TO CHANGE FROM POKEMON 1 TO POKEMON 2
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = Pokemon_1;
temp = transform.localScale; // TO SET THE SCALE WHEN EACH POKEMON CHANGES (works)
temp.x = -380;
temp.y = 350;
transform.localScale = temp;
timer = delay;
finalPosition = new Vector2(167, -107);
transform.position = Vector3.Lerp(transform.position, finalPosition, 1f * Time.deltaTime);
//tempPos = transform.position; // Tried and tested method. Doesn't work.
//tempPos.x = 1f;
//transform.position = tempPos;
return;
}
if (this.gameObject.GetComponent<SpriteRenderer>().sprite == Pokemon_1) // TO CHANGE BACK FROM POKEMON 2 TO POKEMON 1
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = Pokemon_0;
timer = delay;
return;
}
}
On execution, this code does change pokemon and changing the scale. However, the position remains the same.
I am open fairly new so I'm constantly trying our different suggestions, thanks.
Update()
method ? – Cid