It looks like you're new here. If you want to get involved, click one of these buttons!
Hey! Thanks to anyone helping!
I'm working on a 2D platformer. The issue I'm facing is that I have two separate scripts; one for movement and one for attacks. My attack script has a dash like movement which is conflicting with my movement script. I know this because I can run them both separately and they work on their own, but when I run both at the same time, dashing doesn't work.
So, the question is, how can I make my dash work having both scripts active?
This is my movement function, which I call in the FixedUpdate function:
void walk() { // Walk moveInput = Input.GetAxis("Horizontal"); rb.velocity = new Vector2(moveInput * walkSpeed, rb.velocity.y); // Turns when facing opposite direction if (moveInput > 0) { transform.eulerAngles = new Vector3(0, 0, 0); } else if (moveInput < 0) { transform.eulerAngles = new Vector3(0, 180, 0); } }
And this is my dashing script, which I call in the Update function of a different script:
void dashMove() { if (!dashing) { if (Input.GetKeyDown(KeyCode.Space)) { dashing = true; } } else { if (dashTime <= 0) { dashing = false; dashTime = startDashTime; rb.velocity = Vector2.zero; } else { dashTime -= Time.deltaTime; if (transform.eulerAngles.y >= 0) { rb.velocity = Vector2.right * dashSpeed; } else { rb.velocity = Vector2.left * dashSpeed; } } } }
Comments
If you have one script in Update and the other in FixedUpdate they will likely be executing in different frames (at least for many frames). Move them both to a FixedUpdate step (Assuming that you are using physics or animation elsewhere) and I think the behaviour will become consistent.
Usually what I do is have anything physics related in FixedUpdate, while Inputs and such is in the normal Update
I tried changing both to FixedUpdate and it still doesn't work. However it helped me to find the exact part that's giving issues. So, I managed to make it work adding this validation to the movement script:
However it doesn't let me dash while walking, so this is not the expected behaviour. I read somewhere that adding states and enabling/disabling scripts based on current state should work. Could someone confirm if this is all right to do? I mean, it should work, but since dashing is a milliseconds state, I'm not sure if this is a proper solution to my issue. Thanks in advance!