Skip to main content

Drivable car

A physics car with a driver seat and a passenger seat. The script only turns the driver's input into forces; physics gives the car gravity, slopes and collisions, and an ST Object Sync sends the result to everyone.

Set up the car root with:

  • a Rigidbody (not kinematic, gravity on) and a collider with a low-friction Physic Material (friction about 0.05): a box has no wheels, so with normal friction it drags along the floor. The script's grip stops it sliding sideways;
  • an ST Object Sync;
  • two children with an ST Seat each. The driver seat has Take Controls on, so whoever sits there owns the car; set each seat's Exit Location beside the car;
  • SimpleCar, with its Driver Seat set to the driver seat.

The seats carry their players as the car moves, so nothing in the script moves a player. A passenger sees the driver in the next seat, not trailing behind.

It steers like a car: it turns only while rolling, more as it speeds up, and the other way when it really reverses. The stick's forward and back only count past Throttle Dead Zone, because a VR stick pushed sideways also tips a little back and would otherwise brake. The velocity turns with the car, so it keeps its speed through a corner.

SimpleCar.cs
using StageTime.SDK;
using UnityEngine;

/// <summary>
/// A drivable physics car. Put it on the car root with a Rigidbody (not kinematic, gravity on), colliders and an
/// ST Object Sync. The driver seat has Take Controls on, so whoever sits there owns the car; the seats carry their
/// players, so nothing here moves a player. Only the driver's device drives: the move input becomes forces, and
/// physics gives the car gravity, slopes and collisions. It steers like a car: it turns only while rolling, and the
/// other way when reversing. A box has no wheels, so give its collider a low-friction
/// Physic Material; grip stops it sliding sideways, like tyres. Use (left mouse, trigger) makes it hop.
/// </summary>
public class SimpleCar : StageTimeBehaviour
{
public STSeat driverSeat;
public float power = 12f;
public float turnSpeed = 60f;
public float fullTurnSpeed = 3f; // m/s at which the car turns at turnSpeed (degrees per second)
public float throttleDeadZone = 0.3f;
public float grip = 8f;
public float hop = 4f;

private Rigidbody body;

private void Start()
{
body = GetComponent<Rigidbody>();
}

private bool IsLocalDriver()
{
StageTimePlayer driver = driverSeat.seatedPlayer;
return driver != null && driver.isLocal;
}

private void FixedUpdate()
{
if (!IsLocalDriver()) return; // only the driver's device drives the car
Vector2 move = StageTimeInput.move;
// A stick pushed sideways also tips a little forward or back; only a clear push drives or brakes.
float throttle = Mathf.Abs(move.y) < throttleDeadZone ? 0f : move.y;
body.AddForce(transform.forward * (throttle * power), ForceMode.Acceleration);

// Steer like a car: by the way it is rolling, not by the stick, so the turn grows with speed and flips only
// when the car really reverses. Setting the yaw rate also stops spin left over from a crash.
Vector3 up = transform.up;
float forwardSpeed = Vector3.Dot(body.linearVelocity, transform.forward);
float rolling = Mathf.Clamp(forwardSpeed / fullTurnSpeed, -1f, 1f);
float yawRate = move.x * turnSpeed * rolling; // degrees per second
Vector3 spin = body.angularVelocity;
body.angularVelocity = spin - up * Vector3.Dot(spin, up) + up * (yawRate * Mathf.Deg2Rad);

// Tyres: the velocity turns with the car, and grip removes what is still sliding sideways.
body.linearVelocity = Quaternion.AngleAxis(yawRate * Time.fixedDeltaTime, up) * body.linearVelocity;
float sideways = Vector3.Dot(body.linearVelocity, transform.right);
body.AddForce(transform.right * (-sideways * grip), ForceMode.Acceleration);
}

public override void OnInputUse(bool value, STInputArgs args)
{
if (value && IsLocalDriver()) body.AddForce(Vector3.up * hop, ForceMode.VelocityChange);
}
}

A button beside it puts the car back where it started:

RespawnButton.cs
using StageTime.SDK;

/// <summary>
/// Click to put an object with an ST Object Sync (a car that flipped, a ball that rolled away) back where it started,
/// for everyone. Respawn runs on the object's owner, so any player can press it.
/// </summary>
public class RespawnButton : StageTimeBehaviour
{
public STObjectSync target;

public override void Interact()
{
if (target != null) target.Respawn();
}
}