With W-A-S-D keys the character should be moving while an animation is played.
Currently, the character moves in the direction of z-axis, while holding W-key. But the animation doesn’t play correctly, because the key is fired constantly. I know I have to call the function outside/without switch-case, but I don’t know how. Probably with a true/false statement.
There are many forums with a similar problem, but no solution was working for me.
For example:
So my questions are: 1. How do I get the animation played and moving the character only while key is pressed? 2. How do I get the character moving in the direction of the camera? 3. How do I get the run animation played, when w and shift are pressed at the same time?
I hope at least one question can be solved
As always, thank you in advance.
1. How do I get the animation played and moving the character only while key is pressed?
First, you have to listen to the keyboard events :
Example
let isZPressed = false;
let isQPressed = false;
let isSPressed = false;
let isDPressed = false;
document.addEventListener('keydown', (e) => {
if (e.keyCode == 90) { isZPressed = true; }
if (e.keyCode == 81) { isQPressed = true; }
if (e.keyCode == 83) { isSPressed = true; }
if (e.keyCode == 68) { isDPressed = true; }
if (e.keyCode == 66) { isBPressed = true; }
});
document.addEventListener('keyup', (e) => {
if (e.keyCode == 90) { isZPressed = false; }
if (e.keyCode == 81) { isQPressed = false; }
if (e.keyCode == 83) { isSPressed = false; }
if (e.keyCode == 68) { isDPressed = false; }
});
Then in your game loop you have to check if the key is pressed.
If yes, you have to move your entity by multipling the its front vector (normal vector) with its speed.
For now I focused on the animation and movement on the same time.
Sadly, it isn’t working as it should be.
How do I check the key in my game loop?
I used the registerBeforeRender function, because in the runRenderLoop function, I can’t refere to my imported mesh.
Furthermore, only the animation of the else-statement is working, while the object is moving. And only when the if-statement doesn’t contain an animation.
it uses onBeforeRenderObservable for its gameloop and an action manager and dictionary to handle keys. It only calls begin animation if it is not already animating.
Thank you all for your help. The moving was never the problem, more the animation at the same time.
I will also try using vectors to move.
But for my problem, the playground @trevordev posted is solving my problem…well, I think so. I just tried it with my playground and there it works fine.
I also was trying to run the animation while there is no animation, but it didn’t work as it does in this example.
thank you your sample contains a much better key control system then what i had remember i asked for multiple camera key inputs this is way better thx but how would i replace the key name with the keycode