I have just started to create a basic 2D topdown bullet hell game. I have created basic input and I have to create a dash, however my dash is bugged. I cannot dash when I press both the down and left arrow keys. Also, when I hold down left arrow and z (dash button), I cannot move downwards. Every other combination of arrow keys functions properly. Is there a bug or restriction that I am not aware of?
(Yes, TrailRenderer is currently unused.)
I am using the arrow key for movement and z for dash.
Update: It turns out it was my arrow keys, WASD works fine which is strange.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : GameManager
{
public Rigidbody2D playerRb;
public TrailRenderer dashTrail;
[Header("Player Movement")]
public float moveSpeed = 10f;
float horizontalMovement;
float verticalMovement;
[Header("Player Dash")]
public float dashSpeed = 20f;
bool isDashing;
bool canDash = true;
public float dashTimeLength = 0.1f;
public float dashCooldown = 0.2f;
// Start is called before the first frame update
private void Start()
{
}
// Update is called once per frame
void Update()
{
}
void FixedUpdate()
{
if(!IsGameOver())
{
if(!isDashing)
{
playerRb.velocity = new Vector2(horizontalMovement * moveSpeed, verticalMovement * moveSpeed);
}
}
}
public void Dash(InputAction.CallbackContext context)
{
if(context.performed & canDash)
{
StartCoroutine(DashNow());
}
}
public void Move(InputAction.CallbackContext context)
{
horizontalMovement = context.ReadValue<Vector2>().x;
verticalMovement = context.ReadValue<Vector2>().y;
}
IEnumerator DashNow()
{
isDashing = true;
canDash = false;
playerRb.velocity = new Vector2(horizontalMovement * dashSpeed, verticalMovement * dashSpeed);
yield return new WaitForSeconds(dashTimeLength);
playerRb.velocity = new Vector2(0, 0);
isDashing = false;
yield return new WaitForSeconds(dashCooldown);
canDash = true;
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("Enemy"))
{
GameOverNow();
playerRb.velocity = new Vector2(0, 0);
}
}
}