How can you move the tank using the w, a, s, d keys, given where the muzzle of the tank is looking

image

the tank only moves forward and backward, but when I turn it (a, d with the key), it does not move correctly

To little info to go off of unfortunately. “does not move correctly” can mean a wide array of things. What behavior are you getting and what do you expect it to do?

I do not take into account the angle, for example, I will turn the tank 90 degrees to the left, it will go sideways forward

maybe you have something similar so i can figure it out?
car game or smth similar

Thats might be because you are moving the tank relative to the world position and not the camera. To do this you need something like this:

const move = (degRad) => {
    mesh.position.add(
        this.camera.getWorldDirection(new THREE.Vector3())
            .applyAxisAngle(new THREE.Vector3(0,1,0), degRad)
            .multiply(new THREE.Vector3(this.player.speed, 0, this.player.speed))
    );
}
if(this.keys[87]){ // W key
    move(0);
}
if(this.keys[83]){ // S key
    move(Math.PI);
}
if(this.keys[65]){ // A key
    move(Math.PI/2);
}
if(this.keys[68]){ // D key
    move(-Math.PI/2);
}

This is what I had used in my game but it was first person. Not sure if yours is too. Alternatively you could replace the camera in this code with the mesh itself too. So it moves relative to itself.

could you send me the whole code?

As far as the movement goes thats pretty much it.
You can replace this.player.speed with 1 for now and this.keys with event.code === “whateverKey”

image

i did so

almost works, but now the tank does not turn when I press a or d it goes sideways to the right or left

If you want it to turn again then just repalce my code for the a and d keys back to your original code, since you didn’t have any problems with the rotation before as far as I understand.

yes, there are no problems with rotation, but it will not drive correctly, it will turn and when I press w or s it will go in the wrong direction where the gun is looking

You mean it goes backward when it should go forward? If so try swapping move(0) with move(Math.PI)

Try this : fcs.sbcode.net
Demo

1 Like

no, i turn it 90 degrees its muzzle is pointing to the left and it should go to the left, but it goes forward


so he rides