Hi all,
I’m trying to make a basic top down 8 direction player controller.
It’s modified from a tutorial I found a while ago to include a roll function. The roll works in the cardinal directions and diagonally up and right or down and left. For some reason however, the roll doesn’t do anything when moving down and right or up and left. Initially I thought that might have something to do with the positive and negative x and y cancelling each other out somehow but can’t figure out why that would occur. Also the debug (which I’ve left in below) says that the problem relates to recognising the input of jump being pressed (step 2 of the debug isn’t firing). So now I have no idea what to do. Tried building a dummy version to see if it was a weird issue in the editor.
Anyone have any suggestions?
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 60f;
public float dodgeSpeed = 250f;
public Vector3 attackDir = new Vector3(1, 0, 0);
private enum State
{
Normal,
Rolling,
Paused,
Attacking,
}
private Rigidbody2D rb;
private Vector3 moveDir;
private Vector3 rollDir;
private Vector3 lastMoveDir;
private float rollSpeed;
private State state;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
state = State.Normal;
}
private void Update()
{
switch (state)
{
case State.Normal:
float moveX = 0f;
float moveY = 0f;
moveX = Input.GetAxisRaw("Horizontal");
moveY = Input.GetAxisRaw("Vertical");
moveDir = new Vector3(moveX, moveY).normalized;
lastMoveDir = moveDir; //remember what direction you're facing
Debug.Log("Step 1: lastmovedir = : " + lastMoveDir);
if (moveDir != Vector3.zero)
attackDir = moveDir; //look in the direction of the last input
if (Input.GetButtonDown("Jump")) //if roll is pressed
{
Debug.Log("Step 2: jump pressed");
rollDir = lastMoveDir; //roll in direction of movement
Debug.Log("Step 3: rolldir = " + rollDir);
rollSpeed = dodgeSpeed;
state = State.Rolling;
}
break;
case State.Rolling:
float rollSpeedDropMultiplier = 5f; //amount to reduce roll by each second
rollSpeed -= rollSpeed * rollSpeedDropMultiplier * Time.deltaTime;
float rollSpeedMinimum = (dodgeSpeed/5); //the point at which you stop rolling. Playing around, /5 seems to be smoothest
if (rollSpeed < rollSpeedMinimum)
{
state = State.Normal;
}
break;
}
}
private void FixedUpdate()
{
switch (state)
{
case State.Normal:
rb.velocity = moveDir * moveSpeed;
break;
case State.Rolling:
rb.velocity = rollDir * rollSpeed;
break;
}
}
}