Simple tank steer turn using vehicle seat

Hi guys,

Today I want to tell you about a technique I have discovered for making vehicles with tank style turning using vehicle seats. I have done a search and couldn’t find a tutorial on this, despite how easy it is to accomplish!

This tutorial assumes you know how to create objects including parts, welds and hinges, scripts, models and of course the vehicle seat.

Traditionally vehicle seats can be used for conventional cars where one set of wheels pivots side to side for steer and the other set of wheels provide drive. What I’m about to describe will make a vehicle that can turn on a pinhead like a tank by allowing the wheels on each side to drive in opposite directions.

To start off you will want to create four cylinder parts, one block part and your vehicle seat.

Weld the vehicle seat to the top of the block part and use four hinges on the block part to attach the cylinder parts, one on each so the block part forms the body and the cylinder parts form the wheels. Pop your creation into a model to create a result like this:

That is the modelling of the car done. You’ll notice we have also created a script inside the vehicle seat. This is where the magic happens:

-- First up define your parts
local seat = script.Parent
local car = seat.Parent
local leftDrive = car.Body.LeftDrive
local rightDrive = car.Body.RightDrive

--You might have noticed a couple of hinges called left and right steer in the car model. These are just free spinning hinges and don't need defining in this script.

-- Define your variables
local maxSpeed = 10

-- Down bottom we've added a line which will trigger a function when the seat's controls are altered.

-- And here is that function. We've called it 'onChanged'
local function onChanged (property)

--This first 'main if' will do two things.
	if property == "Steer" then

--This first 'sub if' will cause the vehicle to turn left or right depending on input. 
		if seat.Steer > 0 or seat.Steer < 0 then
			leftDrive.AngularVelocity = maxSpeed * seat.Steer
			rightDrive.AngularVelocity = maxSpeed * seat.Steer
		end

-- second 'sub if' will restore the wheels drive back to normal after the player stops attempting to steer. This is important if the player wants to continue to drive forwards or backwards after stopping steering.
		if seat.Steer == 0 then
			leftDrive.AngularVelocity = maxSpeed * seat.Throttle
			rightDrive.AngularVelocity = -maxSpeed * seat.Throttle	
		end
	end

-- This second 'main if' simply drives the vehicle forward or back depending on input
	if property == "Throttle" then
		leftDrive.AngularVelocity = maxSpeed * seat.Throttle
		rightDrive.AngularVelocity = -maxSpeed * seat.Throttle
	end
	
end

seat.Changed:Connect(onChanged)

The trick to getting this to work, is to use seat.Steer as a proxy for seat.Throttle when you want to make your vehicle turn. Notice when making the vehicle drive forward or backwards, to ensure the right Drive wheel is turning in agreement with the left, it needs to be turned into a negative number.

Make sure your parts can collide, aren’t anchored, hit test, report your results!

robloxapp-20201016-1900354.wmv (6.0 MB)

Summary

Be gentle, I’m new.

16 Likes

Pretty simple, but nice. The only thing I’d replace is .Changed with :GetPropertyChangedSignal(“Steer”) and :GetPropertyChangedSignal(“Throttle”) as they’ll only fire when the selected property’s value changes. It removes the need for an if-check and removes unnecessary calls.

2 Likes

It’d probably be better to just use Heartbeat for a smoother experience. This is player input you’re talking about, it should be prioritized.

3 Likes

I’m only familiar with Heartbeats as a song by The Knife. Any chance you could give us the skinny on what it means in the context of Roblox and how to use it to make a simple tank turn vehicle?

2 Likes

I would highly recommend you take a look at this article:

and this API:

I’d say its one of the most important Roblox APIs to be familiar with!

You’d simply connect a function to Heartbeat that adjusts the velocities appropriately. I’d recommend you put this in a LocalScript.

4 Likes

How would I put this into a tank tracked vehicle? Can you provide an example using the script at the top please? I’m very confused.

basically a loop which runs every frame of the game or something, while true do but faster and better, make sure you dont do ludicrous things in it, it runs FAST