---
description: You can program custom wheeled vehicles easily in JU TPS
---

# Coding Custom Wheeled Vehicles

## How to create a Custom Wheeled Vehicle script

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



Use this as example:

{% code title="CustomVehicleExample.cs" lineNumbers="true" %}
```csharp
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);
            }
        }
    }
}
```
{% endcode %}

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. &#x20;

<div align="center"><figure><img src="https://2945806600-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCQMDx0LmgnRXXTklLhoh%2Fuploads%2FwnU22QOSqj3Ds1stdqka%2F2024-03-2119-16-26-Optimized-ezgif.com-crop.gif?alt=media&#x26;token=ab5456d2-535e-4e31-95ca-25fbfb04c4df" alt=""><figcaption></figcaption></figure></div>

## Vehicle Coding Scripting Reference

<table><thead><tr><th width="210">Properties</th><th width="116">Access</th><th>Description</th></tr></thead><tbody><tr><td>Steer</td><td>get, set</td><td>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.</td></tr><tr><td>Throttle</td><td>get, set</td><td>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.</td></tr><tr><td>Brake</td><td>get, set</td><td>A value between 0 and 1 where 0 is without brake and 1 is full brake.</td></tr><tr><td>CanTurnToUpInAir</td><td>protected get, set</td><td>When true, the vehicle will turn when <code>IsGrounded</code> is false to align the <code>Up</code> with if <code>RotateToUpInAirSpeed</code> is greater than 0.</td></tr><tr><td>FinalSteer</td><td>get</td><td>Returns the steer value similar to <code>Steer</code> input but with smooth provided from <code>DrivePadSmooth</code>.</td></tr><tr><td>FinalThrottle</td><td>get</td><td>Returns the throttle value similar to <code>Throttle</code> input but with smooth provided from <code>DrivePadSmooth</code>.</td></tr><tr><td>FinalBrake</td><td>get</td><td>Returns the brake value similar to <code>Brake</code> input but with smooth provided from <code>Throttle</code>.</td></tr><tr><td>IsGrounded</td><td>get</td><td>Returns true if the vehicle is grounded.</td></tr><tr><td>WheelHit</td><td>get</td><td>Return ground hit info if <code>IsGrounded</code> is true.</td></tr><tr><td>LocalVelocity</td><td>get</td><td>Returns the relative vehicle velocity.</td></tr><tr><td>WheelsUpDirection</td><td>get</td><td>Returns the vehicle up based on the median direction of all wheels positions.</td></tr><tr><td>CurrentSteerVsSpeed</td><td>get</td><td>Returns the <code>SteerVsSpeed</code> value relative to the current velocity. Used to improve the control on high speeds, decreasing the steer to make curves more smoothly.</td></tr><tr><td>RigidBody</td><td>get</td><td>The rigidbody body of this vehicle.</td></tr><tr><td>ForwardSpeed</td><td>get</td><td>Returns the vehicle forward speed.</td></tr></tbody></table>



{% hint style="info" %}
You can override the base methods to create custom vehicle logic.
{% endhint %}

{% hint style="danger" %}
Don't forgot to call the base methods inside the `override`.
{% endhint %}

<table><thead><tr><th width="196">Override Methods</th><th>Description</th></tr></thead><tbody><tr><td>OnValidate </td><td>Used only by the editor, called after change some property on the editor to validate variables.</td></tr><tr><td>Awake</td><td>Called when the scene load.</td></tr><tr><td>Start</td><td>Called when the game starts, after <code>Awake</code></td></tr><tr><td>FixedUpdate</td><td>Called each physx update before Update.</td></tr><tr><td>Update</td><td>Called on each frame updating vehicle logic and controls.</td></tr></tbody></table>

<table><thead><tr><th width="432">Methods</th><th>Description</th></tr></thead><tbody><tr><td>AddForwardAcceleration(<mark style="color:blue;">float</mark> accelerationForce)</td><td>Add forward acceleration applying a force to the <code>Rigidbody</code> to forward.</td></tr><tr><td>SimulateAntiRollBar(<mark style="color:blue;">float</mark> antiRollForce, <mark style="color:green;">WheelCollider</mark> LeftWheel, <mark style="color:green;">WheelCollider</mark> RightWheel)</td><td>Simulate car anti roll bar</td></tr><tr><td>SetVehicleCenterOfMass(<mark style="color:blue;">Transform</mark> position)</td><td>Set vehicle center of mass</td></tr><tr><td>SimulateGroundAlignment(<mark style="color:blue;">float</mark> alignmentSpeed)</td><td>Align vehicle to ground</td></tr><tr><td><mark style="color:blue;">Vector3</mark> GetExitPosition()</td><td>Get character exit position</td></tr></tbody></table>

| VehicleGizmos Class Functions                                                                                                                                                                                           |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| DrawVector3Position(<mark style="color:blue;">Vector3</mark> position, <mark style="color:blue;">Transform</mark> Vehicle, <mark style="color:blue;">string</mark> Label, <mark style="color:blue;">Color</mark> color) |
| DrawVehicleInclination(<mark style="color:blue;">Transform</mark> RotationParent, <mark style="color:blue;">Transform</mark> RotationChild)                                                                             |
| DrawRaycastHit(<mark style="color:purple;">VehicleRaycastCheck</mark> rayCheck, <mark style="color:blue;">Transform</mark> vehicle, <mark style="color:blue;">Vector3</mark> direction)                                 |
| DrawOverturnCheck(<mark style="color:purple;">VehicleOverturnCheck</mark> OverturnCheck, <mark style="color:blue;">Transform</mark> Vehicle)                                                                            |
| DrawOverlapBoxCheck(<mark style="color:purple;">VehicleOverlapBoxCheck</mark> BoxCheck, <mark style="color:blue;">Transform</mark> Vehicle)                                                                             |
| DrawVehicleGroundCheck(<mark style="color:purple;">VehicleGroundCheck</mark> GroundCheck, <mark style="color:blue;">Transform</mark> Vehicle)                                                                           |
