Question by
thundafellow · Mar 10, 2019 at 02:59 AM ·
movement2d-platformer2d spritesflip
Help with flipping 2d sprite when changing directions
I'm building a 2D sidescrolling platformer as a way to learn Unity. I'm trying to write code for the player's movement that causes the sprite to flip over the x axis whenever I change directions (e.g. it faces left when going left, and right when going right). Below is my movement code. I'm very, very new to this and have browsed the forums for a while and haven't found a solution for my specific issue.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class movementScript : MonoBehaviour { public int movementspeed = 100;
void Start()
{
}
void Update()
{
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * movementspeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right * movementspeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Space))
{
transform.Translate(Vector3.up * movementspeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
}
}
Comment