Mesh movement is

Good day, Babylonians!

I’m trying to implement 3rd person movement in my project. Could you look at my PG and help me to fix one thing?

When I press W key, the mesh does big step forward and after that moves smoothly as I expect. When I release W key, the mesh does big step backward and after that stops. It also works in that way with A, S, D keys. Looks awful and not natural.

https://www.babylonjs-playground.com/#VWVU8B

I’d like to move mesh smoothly and control its speed by variable. Probably I miss something.

Thanks for your time :innocent:

Look into Lerping the position not directly scaling and adding.

Could you send me any examples, please? I have no idea what are you talking about :slight_smile:

Thanks

I have like 10 mins here, let me see if I can whip up something.

https://www.babylonjs-playground.com/#VWVU8B#5

Sorry had a phone call I had to take, but here you go.

Actually I see the jump still… to fix that we would need to switch to an “Inertia based” calculation.

So basically have your direction vector that gets scaled by the inertia, and then when there is no input start scaling the inertia value down.

I have to get to work now so I don’t have time to do anymore for you sorry!

1 Like

Thank you, sir! I appreaciate your help.

I’ll try to find some information about ‘intertia base’ calculation.

just have a acceleration value that you add to a “speed” variable then scale your vector by the speed variable.

Add the acceleration to the speed variable on key inputs (to a certain max) then when there is no input multiple the speed by like 0.86 or something and continue to always displace the positions based on the speed and last input vector.

@Pryme8
Could you look at my code, please? I dont quite understand math of movement. Im trying to make it but the result is sad.

	if (inputMap.w) {
		let vector = Babylon.Vector3.Forward();
		this.speed += this.acceleration * deltaTime;

		if (this.speed > this.maxSpeed) {
			this.speed = this.maxSpeed;
		}

		this.mesh.position.addInPlace(vector.scale(deltaTime));
	}

https://www.babylonjs-playground.com/#VWVU8B#7

2 Likes

I dont understand how it works :frowning:
But it does. Thanks!