I’m working on a 2d Game where my character can dodge via sliding… when I normalize my input I’m not able to dodge in the Up Left, Dow Left, Dow Right… but I’m able to dodge Up right… and I can also dodge on the normal 4 axis. can someone please explain this strange issue and help me correct it thank you!
public void HandleMovement()
{
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
input = input.normalized;
animator.SetFloat("Speed", input.sqrMagnitude);
bool isIdle = input.x == 0 && input.y == 0;
if (isIdle)
{
animator.SetFloat("LastInputX", lastInput.x);
animator.SetFloat("LastInputY", lastInput.y);
}
else
{
playerRigidbody.MovePosition(transform.position + input * walk * Time.deltaTime);
animator.SetFloat("Horizontal", input.x);
animator.SetFloat("Vertical", input.y);
lastInput = input;
}
}
private void HandleDodging()
{
if (Input.GetKeyDown(KeyCode.Space) && input.x == 1 | input.y == 1 | input.x == -1 | input.y == -1)
{
state = State.Dodging;
isDodging = true;
}
}
private void Dodging()
{
playerRigidbody.MovePosition(transform.position + input * dodgeSpeed * Time.deltaTime);
dodgeSpeed -= dodgeSpeed * dodgeSpeedReduced * Time.deltaTime;
if (dodgeSpeed < dodgeSpeedMinimum)
{
state = State.Normal;
dodgeSpeed = dodge;
isDodging = false;
}
}
@Drayanlia if you want to give up on me I understand but it’s still not working and its not checking for space in the bool.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
public class PlayerController : MonoBehaviour
{
public Animator animator;
private Rigidbody2D playerRigidbody;
private Vector3 input;
private Vector3 lastInput;
public float walkSpeed;
float walk;
public float dodgeSpeed;
float dodge;
public float dodgeSpeedReduced;
float dodgeSpeedMinimum;
public bool isDodging;
public bool spacePressed;
private State state;
private enum State
{
Normal, Dodging
}
public void Awake()
{
playerRigidbody = GetComponent<Rigidbody2D>();
walk = walkSpeed;
state = State.Normal;
dodgeSpeedMinimum = walk;
dodge = dodgeSpeed;
}
private void Update()
{
HandleMovement();
if (Input.GetKeyDown(KeyCode.Space))
{
spacePressed = true;
}
}
public void FixedUpdate()
{
switch (state)
{
case State.Normal:
HandleDodging();
break;
case State.Dodging:
Dodging();
break;
}
spacePressed = false;
}
public void HandleMovement()
{
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
animator.SetFloat("Speed", input.sqrMagnitude);
animator.SetFloat("LastInputX", lastInput.x);
animator.SetFloat("LastInputY", lastInput.y);
animator.SetFloat("Horizontal", input.x);
animator.SetFloat("Vertical", input.y);
}
private void HandleDodging()
{
bool isIdle = input.x == 0 && input.y == 0;
if (spacePressed & (input.x == 1 | input.y == 1 | input.x == -1 | input.y == -1))
{
state = State.Dodging;
isDodging = true;
}
else if (!isIdle)
{
playerRigidbody.MovePosition(transform.position + input.normalized * walk * Time.fixedDeltaTime);
lastInput = input;
}
}
private void Dodging()
{
playerRigidbody.MovePosition(transform.position + input * dodgeSpeed * Time.fixedDeltaTime);
dodgeSpeed -= dodgeSpeed * dodgeSpeedReduced * Time.deltaTime;
if (dodgeSpeed < dodgeSpeedMinimum)
{
state = State.Normal;
dodgeSpeed = dodge;
isDodging = false;
}
}
}