Okay, so I want to dash a certain length if I press a button, yadda yadda yadda. However, when my code is in ONLY Update(), the length that I dash is inconsistent. If I swap Update() with FixedUpdate(), my button presses dont register half the time. Is there any way to fix this? I have been on it for days.
(Btw im a noob with my first game so please go easy on me!)
Here is my dash script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dash : MonoBehaviour
{
private Rigidbody2D rb;
public float dashSpeed;
private float dashTime;
public float startDashTime;
private int direction;
void Start()
{
rb = GetComponent<Rigidbody2D>();
dashTime = startDashTime;
}
// How would I fix this?
void FixedUpdate()
{
if (direction == 0)
{
if (Input.GetKeyDown(KeyCode.C))
{
direction = 1;
}
else if (Input.GetKeyDown(KeyCode.V))
{
direction = 2;
}
}
else
{
if (dashTime <= 0)
{
direction = 0;
dashTime = startDashTime;
rb.velocity = Vector2.zero;
}
else
{
dashTime -= Time.deltaTime;
if (direction == 1)
{
rb.velocity = Vector2.left * dashSpeed;
}
else if (direction == 2)
{
rb.velocity = Vector2.right * dashSpeed;
}
}
}
}
}