Help with movement and replication

I’m making a top-down tank game like diep.io, with the camera fixed to the tank (no camera rotation), the tank barrel should follow the mouse and WASD keys are used for movement along the global X and Z axis.

The camera bit is finished, when a player joins, a tank is cloned from ServerStorage, and the player’s camera is moved off the character to above the tank’s base, with a CFrame update on RenderStepped, so it follows the movement of the tank.

As for movement and rotation, I got the best results with the tank’s base anchored above the ground, and connecting RenderStepped on the client to a RemoteEvent that updates the CFrame of the tank on the server according to the mouse Hit position (using tank’s Y position), and the server also received two parameters velocityX and velocityZ that are either -1, 0 or 1, depending on which of WASD keys are pressed.

However this made the rotation lag while the tank is moving, would probably choke the server with more than a few players, and I’d have to add additional checks to see if the tank position is out of bounds (instead of having a wall for tanks to collide with) which would surely choke the server.

Plus, I don’t want instant movement on WASD keypresses, rather a gradual increase/decrease in speed like in diep. This would call for a more complex script, when I know there are BodyMovers that can achieve this and replicate automatically without RemoteEvents. But I didn’t have much luck with BodyMovers either, I don’t know much about them.

So in short: I need something that will move the tanks only on global X and Z, that will have smooth gradual speed increase/decrease, and allow having a final bound on the map which the tank can’t cross. The tank should rotate on the global Y axis towards where the mouse is pointing, even when the tank is moving, but X and Z rotation should be locked. And I need all of this to replicate smoothly to all clients… I think a few frames of lag should be fine, but nothing more than that, as everyone needs to know where to aim and stuff.

Any suggestions are greatly appreciated.

3 Likes

Well, you can actually use Humanoids to handle the movement using @ThanksRoBama lerping method to gradually accelerate the humanoid using the joystick properties of the Humaniod:Move() in a render step.

Maybe try it out just copy and paste it into your player scripts and it see if you can use it for your game.

Plus there is code to handle the global XZ depending on how the camera is facing like a normal humanoid which should be static for a game like the io game always birds eye view on top of the character.

local function getWalkDirectionWorldSpace()
	local walkDir = camera.CFrame:VectorToWorldSpace( getWalkDirectionCameraSpace() )
	walkDir *= Vector3.new(1, 0, 1) --Set Y axis to 0

	if walkDir.Magnitude > 0 then --(0, 0, 0).Unit = NaN, do not want
		walkDir = walkDir.Unit --Normalize, because we (probably) changed an Axis so it's no longer a unit vector
	end

	return walkDir
end

As for replication, there will always be a delay but I think you can hide it by making the turrets aim where they are shooting based on the client’s individual perspective perhaps a fire all clients on bullet shot. Although I don’t think it matters gameplay-wise.

2 Likes

I was actually thinking about this, if I understood right, you’re suggesting that I change the Character’s body into the tank, instead of moving the camera to the tank?

1 Like

Yeah I was thinking that the character should be the tank right? But… you could also move the camera to the tank in order to get the move direction relative to the worlds XZ axis.

But you could do it anyway you want. For my purposes I made it relative to the humanoid root part look vector to move my mech so that it has some acceleration properties.

The rotation is using a constant torque but I’m planning on lerping that too so it accelerates left to right as well.

If you do that, you won’t have to use a custom camera controller because the default one follows whatever you’ve set the player’s character to. If you want a top-down view you’ll still need to use your custom camera controller.

1 Like

All this would make rotation not work as intended though, if I press A or D the tank would turn to left or right. Any way to overwrite this?

oof1

I followed the official instructions, they just forgot to mention to add a Humanoid object inside the model. This would be great, but can anyone help me with overwriting the ControlModule to get what I want?