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

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

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

Last updated