So, I’ve been making a turn based dungeon game, and trying to get the player to move properly based off of turn based movement has proven to be very difficult, here’s my script
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
private bool IsMoving = false;
public Animator animator;
private bool flipped = false;
private Vector3 positionStore = Vector3.zero;
IEnumerator PlayerMoveUp()
{
IsMoving = true;
yield return new WaitForSeconds(0.15f);
IsMoving = false;
}
IEnumerator PlayerMoveDown()
{
IsMoving = true;
yield return new WaitForSeconds(0.15f);
IsMoving = false;
}
IEnumerator PlayerMoveRight()
{
IsMoving = true;
yield return new WaitForSeconds(0.15f);
IsMoving = false;
}
IEnumerator PlayerMoveLeft()
{
IsMoving = true;
yield return new WaitForSeconds(0.15f);
IsMoving = false;
}
private void Update()
{
if (!IsMoving)
{
if (Input.GetKey(KeyCode.UpArrow))
{
animator.SetBool("IsFacingForwards", false);
animator.SetBool("IsFacingSide", false);
animator.SetBool("IsFacingBackwards", true);
StartCoroutine(PlayerMoveUp());
transform.Translate(Vector2.up);
} else if (Input.GetKey(KeyCode.DownArrow))
{
animator.SetBool("IsFacingForwards", true);
animator.SetBool("IsFacingSide", false);
animator.SetBool("IsFacingBackwards", false);
StartCoroutine(PlayerMoveDown());
transform.Translate(Vector2.down);
} else if (Input.GetKey(KeyCode.RightArrow))
{
if (flipped)
{
transform.Rotate(0, 180, 0);
}
animator.SetBool("IsFacingForwards", false);
animator.SetBool("IsFacingSide", true);
animator.SetBool("IsFacingBackwards", false);
StartCoroutine(PlayerMoveRight());
transform.Translate(Vector2.right);
} else if (Input.GetKey(KeyCode.LeftArrow))
{
if (!flipped)
{
transform.Rotate(0, 0, 0);
}
animator.SetBool("IsFacingForwards", false);
animator.SetBool("IsFacingSide", true);
animator.SetBool("IsFacingBackwards", false);
StartCoroutine(PlayerMoveLeft());
transform.Translate(Vector2.left);
}
}
}
}
right now the script will teleport the player to the position I want them to go, rather than moving them normally (I already know why that’s part’s happening, I just mentioned it because I can’t figure how to write normal movement, but still get it to work turn based), although, the part I can’t figure out, is that the player is supposed to change their animation when they move, so for example, if the player is facing upwards, and I move them to the side, they should immediately change to the side animation, but it will wait 0.15 seconds rather than being immediate, which is weird because I didn’t put the code in the cororoutine, does anyone know how to write this script correctly?