I have a movement that looks like this:
void FixedUpdate()
{
if (canMove == true)
{
moveInput = Input.GetAxisRaw("Horizontal") * moveSpeed;
}
if (knockbackCount <= 0 && canMove == true)
{
rb.velocity = new Vector2(moveInput * Time.fixedDeltaTime, rb.velocity.y);
}
It has been working fine, but I have a problem with movement from other sources. If I were to get knocked back or use a separate movement option, such as a dash from this script:
public Rigidbody2D rb;
public PlayerMove pm;
public float xDashSpeed;
public float yDashSpeed;
public int direction;
public bool canDash;
public int whatDash;
public float dashLength;
public bool isDashing;
void Start()
{
rb = GetComponent<Rigidbody2D>();
pm = GameObject.Find("Player").GetComponent<PlayerMove>();
isDashing = false;
canDash = true;
}
void Update()
{
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
direction = 1;
}
else if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
direction = 2;
}
else if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
direction = 3;
}
else if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
{
direction = 4;
}
else
{
direction = 0;
}
if (Input.GetKeyDown(KeyCode.C) && canDash == true)
{
pm.canMove = false;
isDashing = true;
if (direction == 0 || direction == 1 || direction == 2)
{
whatDash = 0;
}
else if (direction == 3)
{
whatDash = 1;
}
else if (direction == 4)
{
whatDash = 2;
}
}
if (isDashing == true)
{
if (direction == 0)
{
if (pm.transform.eulerAngles.y == 180)
{
rb.velocity = new Vector2(-xDashSpeed, 0);
}
else if (pm.transform.eulerAngles.y == 0)
{
rb.velocity = new Vector2(xDashSpeed, 0);
}
}
else if (direction == 1)
{
rb.velocity = new Vector2(-xDashSpeed, 0);
}
else if (direction == 2)
{
rb.velocity = new Vector2(xDashSpeed, 0);
}
else if (direction == 3)
{
rb.velocity = new Vector2(0, yDashSpeed);
}
else if (direction == 4 && pm.isGrounded == false)
{
rb.velocity = new Vector2(0, -yDashSpeed);
}
else
{
whatDash = 100;
}
Invoke("EndDash", dashLength);
}
if (pm.isGrounded == true)
{
canDash = true;
}
}
public void EndDash()
{
isDashing = false;
rb.velocity = new Vector2(0, 0);
pm.canMove = true;
if (pm.isGrounded == false)
{
canDash = false;
}
}
I could still affect my x-axis by moving the left and right, even when canMove = false. Is there any way to either temporarily disable this, or use a different movement option?