My code will not work

I need help with my walking mechanic in my game which does not work (It is third person) right now because of a bug that I can not figure out.

Every time I press W then let go then press it again my character’s speed goes 2 times faster.

Hi @duskk,

In your code, when you press the w key down then your character will move for 0.75 seconds forward forever. Even when you release the w key, this animation will continue since the transition-block is inside of a forever block.

Here are some ways to fix this

1. Use the “when key is pressed” CoBlock

Selecting the “pressed” option will cause the code inside to run as often as possible as long as you keep holding down the button.

Code example

Click to show CoBlocks

Video example

2. Stop the forever-loop when you release the w - key

You can do this by creating another variable that checks if you are moving forward. Whenever you are not moving forward (by releasing the w - key), the forever loop is stopped. You can do that by using the if … else block to check for the variable and the break out of loop block.

Code example

Click to show CoBlocks

Note: When setting the isWalking variable to false we’re also using the stop transition block to stop the item movement.

Video example

Below you can find a remixable space in which you can try and test the CoBlocks code yourself :slight_smile:

Hope this helps!

1 Like