Vector3 movement not working for me

I have this code that moves the player (a cube mesh) depending on the key pressed, heres a simplified version:

notes: moveDir is a Vector3

moveDir.addInPlaceFromFloats(-1 * engine.getDeltaTime(), 0, 0);

moveDir.normalize();

player.moveWithCollisions(moveDir);

Now my issue is when I move, its really fast and the speed doesnt change when I change the number (-1)
And when I remove normalize, the player just flies away faster than light and leaves the skybox

Also, if theres a better way to make character movement please tell me

Thanks

Hello @Mhmod , how are you doing?

Can you share a little playground with your demo?

Also, can you try removing the .normalize() and replace the -1 with a very small number, such as 0.001?

Welcome abroad!

getDeltaTime will return the time in miliseconds between frames, which would be at least around 16 for a 60FPS scene. If your scene is small, this will surely make your player move really fast. What you probably want instead is to define some velocity for the player, and perform the necessary unit conversions for it to make sense. For example, assuming that the units in your scene represent meters, if you want your player to move at 1 km/h, that is 1000m/360000ms = 0.0027m/ms.

You also don’t want to add the delta to the movement direction, as that will change the movement direction vector itself. What you want instead is to scale the normalized direction vector by that delta.

Taking the above two points into account, the modified code would look like this:

var speed = 0.0027;
moveDir.normalize();
moveDir.scaleInPlace(speed * engine.getDeltaTime());
player.moveWithCollisions(moveDir);

As Sergio mentioned, if you can share a playground, we can take a better look at your code.

1 Like

Wouldnt scale in place scale all the axis? (x y and z)

@srzerbetto @carolhmj

I tried turning my code into a playground and it wont run for some reason:

Modify the code so the playground didn’t report error now.

It seems it’s working as you expected.

Yes, thank you
So my two problems are:

  • the diagonal movement is way too fast
  • when you move diagonally (two keys) then switch to one key only, its stuck in the same direction

The diagonal movement being fast is a result of how you apply the movement vector. If you move only on one direction, let’s say x, the vector is: (s, 0, 0), where s is your speed. If you move in x and y at the same time (so, diagonal movement), the vector is (s, s, 0). The thing is, these vectors have different lengths, so they represent different speeds, the second one is faster. If you want the character to move always at the same speed, no matter the direction, it’s good to start with a normalized vector, i.e, a vector of length 1. In the x direction, that would be (1,0,0), while in the xy diagonal, that would be (0.707, 0.707, 0) - not (1,1,0)!. Then, you scale the normalized vector by the speed s, and since the starting length is 1, scaling by s would make its length s! Then, all directions will have the same length, which means they also have the same speed.

Hope this explanation makes sense, please let me know if not. :slight_smile:

Okay, thank you! I’ll try that in a bit

What about the other issue where the diagonal movement gets stuck?

Have you checked if the keys in your inputMap are being properly cleared?

Theyre cleared when I let go of the key, but when I hold the key theres multiple duplicates of the key
It wont matter though because the thing that checks for keys uses Array.includes()

And have you checked moveDir?

I added console.log()s for the moveDir and the keysPressed, i have no idea what i broke this time