I’m doing a 2d game where the character walks in four directions but I do not know how to make him change the sprite depending on which direction he goes (sorry if it’s not well written I do not speak English)
this is the player script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
private Animator anim;
public Transform player;
public float speed;
// Use this for initialization
void Start () {
anim = player.GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
Move();
}
void Move()
{
if (Input.GetAxis("Horizontal") > 0)
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
if (Input.GetAxis("Horizontal") < 0)
{
transform.Translate(Vector2.left * speed * Time.deltaTime);
}
if (Input.GetAxis("Vertical") > 0)
{
transform.Translate(Vector2.up * speed * Time.deltaTime);
}
if (Input.GetAxis("Vertical") < 0)
{
transform.Translate(Vector2.down * speed * Time.deltaTime);
}
}
}
You need to change the rotation of the transform based on his movement, something like this:
if (Input.GetAxis("Horizontal") > 0)
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
transform.rotation = Quaternion.Euler(0, 0, 90f);
}
Animator.SetBool(“GoingUp”, true); // When he is going up
Animator.SetBool(“GoingDown”, true); // When he is going down, etc.