Coding Custom Wheeled Vehicles

You can program custom wheeled vehicles easily in JU TPS

How to create a Custom Wheeled Vehicle script

Create a script that will contain the code of the vehicle.

Use this as example:

CustomVehicleExample.cs
using UnityEngine;
using JUTPS.JUInputSystem;

namespace JUTPS.VehicleSystem
{
    // >>> Inherit the Vehicle class to use functions and override methods
    public class CustomVehicleExample : Vehicle
    {
        [System.Serializable]
        public struct Wheel
        {
            public WheelCollider Collider;
            public Transform Mesh;
            [Range(-180, 180)] public float SteerAngle;
            [Range(0, 1)] public float ThrottleIntensity;
            [Range(0, 1)] public float BrakeIntensity;
        }

        public Wheel[] Wheels;

        // Override the Update to pass the player controls to the vehicle.
        protected override void Update()
        {
            base.Update();

            if (!IsOn)
                return;

            //Set default inputs
            if (UseDefaultInputs)
            {
                Steer = JUInput.GetAxis(JUInput.Axis.MoveHorizontal);
                Throttle = JUInput.GetAxis(JUInput.Axis.MoveVertical);
                Brake = JUInput.GetButton(JUInput.Buttons.JumpButton) ? 1 : 0;
            }
        }

        // Override this method to update the wheels data of the vehicle.
        // This pass all necessary data of each wheel to the vehicle compute the
        // acceleration, brake and steer.
        public override void UpdateWheelsData()
        {
            base.UpdateWheelsData();

            WheelsData = new WheelData[Wheels.Length];
            for (int i = 0; i < Wheels.Length; i++)
            {
                Wheel w = Wheels[i];
                WheelsData[i] = new WheelData(w.Collider, w.Mesh, false, w.ThrottleIntensity, w.BrakeIntensity, w.SteerAngle);
            }
        }
    }
}

This code is an example how you can create your our wheeled vehicle, with engine, brake and steer.

All base system for wheeled vehicles are provided from Vehicle class.

You must only set the vehicle controls, overriding Update and pass the wheels to vehicle backend using the UpdateWheelsData.

After setup this code to the vehicle and assigned all wheels, you will have a full controllable vehicle.

Vehicle Coding Scripting Reference

Properties
Access
Description

Steer

get, set

The steer input to control the vehicle move direction. A value between -1 to 1 where -1 is to turn to left, 0 to not turn and 1 is to move to right.

Throttle

get, set

The throttle input to control the vehicle engine acceleration. A value between -1 to 1 where -1 is to move to backward, 0 to be stoped and 1 to move to forward.

Brake

get, set

A value between 0 and 1 where 0 is without brake and 1 is full brake.

CanTurnToUpInAir

protected get, set

When true, the vehicle will turn when IsGrounded is false to align the Up with if RotateToUpInAirSpeed is greater than 0.

FinalSteer

get

Returns the steer value similar to Steer input but with smooth provided from DrivePadSmooth.

FinalThrottle

get

Returns the throttle value similar to Throttle input but with smooth provided from DrivePadSmooth.

FinalBrake

get

Returns the brake value similar to Brake input but with smooth provided from Throttle.

IsGrounded

get

Returns true if the vehicle is grounded.

WheelHit

get

Return ground hit info if IsGrounded is true.

LocalVelocity

get

Returns the relative vehicle velocity.

WheelsUpDirection

get

Returns the vehicle up based on the median direction of all wheels positions.

CurrentSteerVsSpeed

get

Returns the SteerVsSpeed value relative to the current velocity. Used to improve the control on high speeds, decreasing the steer to make curves more smoothly.

RigidBody

get

The rigidbody body of this vehicle.

ForwardSpeed

get

Returns the vehicle forward speed.

You can override the base methods to create custom vehicle logic.

Don't forgot to call the base methods inside the override.

Override Methods
Description

OnValidate

Used only by the editor, called after change some property on the editor to validate variables.

Awake

Called when the scene load.

Start

Called when the game starts, after Awake

FixedUpdate

Called each physx update before Update.

Update

Called on each frame updating vehicle logic and controls.

Methods
Description

AddForwardAcceleration(float accelerationForce)

Add forward acceleration applying a force to the Rigidbody to forward.

SimulateAntiRollBar(float antiRollForce, WheelCollider LeftWheel, WheelCollider RightWheel)

Simulate car anti roll bar

SetVehicleCenterOfMass(Transform position)

Set vehicle center of mass

SimulateGroundAlignment(float alignmentSpeed)

Align vehicle to ground

Vector3 GetExitPosition()

Get character exit position

VehicleGizmos Class Functions

DrawVector3Position(Vector3 position, Transform Vehicle, string Label, Color color)

DrawVehicleInclination(Transform RotationParent, Transform RotationChild)

DrawRaycastHit(VehicleRaycastCheck rayCheck, Transform vehicle, Vector3 direction)

DrawOverturnCheck(VehicleOverturnCheck OverturnCheck, Transform Vehicle)

DrawOverlapBoxCheck(VehicleOverlapBoxCheck BoxCheck, Transform Vehicle)

DrawVehicleGroundCheck(VehicleGroundCheck GroundCheck, Transform Vehicle)

Last updated