Vehicle

https://i.imgur.com/IzVg8VU.jpg

Vehicles are 4-wheeled entities which Characters can possesses and drive. Any Skeletal Mesh can be used to create a Vehicle, although only Skeletal Meshes with Wheels bones can use the built-in feature of animated Wheels.

Note

Currently only 4-Wheeled vehicles is supported.

Tip

Please take a look at our Default’s Vehicle package with all built-in Vehicles already properly configured and ready to use: https://github.com/nanos-world/nanos-world-vehicles.

Usage

-- Spawns a SUV Vehicle
local vehicle = Vehicle(Vector(0, 0, 100), Rotator(), "NanosWorld::SK_SUV", 0, true, false, true, "NanosWorld::A_Vehicle_Engine_14")

-- Configure it's Engine Speed and Steering Wheel location
vehicle:SetEngineSetup(4500)
vehicle:SetSteeringWheelSetup(Vector(0, 20, 131), 26)

-- Configures each Wheel
vehicle:SetWheel(0, "Wheel_Front_Left",  35, 22, 50, 20, 0.25, 2, 17, 1000, 50, 50, 10)
vehicle:SetWheel(1, "Wheel_Front_Right", 35, 22, 50, 20, 0.25, 2, 17, 1000, 50, 50, 10)
vehicle:SetWheel(2, "Wheel_Rear_Left",   35, 22,  0, 20, 0.25, 2, 17, 1000, 50, 50, 10)
vehicle:SetWheel(3, "Wheel_Rear_Right",  35, 22,  0, 20, 0.25, 2, 17, 1000, 50, 50, 10)

-- Adds 4 Doors/Seats
vehicle:SetDoor(0, Vector( 22, -80, 120), Vector(-18, -47, 110), Rotator(0, 0, 15), 75, -150)
vehicle:SetDoor(1, Vector( 22,  80, 120), Vector( 5,   39,  90), Rotator(0, 0,  0), 60,  150)
vehicle:SetDoor(2, Vector(-80, -80, 120), Vector(-60, -39,  90), Rotator(0, 0,  0), 60, -150)
vehicle:SetDoor(3, Vector(-80,  80, 120), Vector(-60,  39,  90), Rotator(0, 0,  0), 60,  150)

-- Make it ready (so clients only create Physics once and not for each function call above)
vehicle:RecreatePhysics()

Constructor Parameters

Type

Name

Default

Vector

location

Vector(0, 0, 0)

Rotator

rotation

Rotator(0, 0, 0)

string

model_name (Skeletal Mesh)

CollisionType

collision_type

CollisionType.Normal

boolean

gravity_enabled

true

boolean

auto_create_physics (can be disabled to improve performance when setting several configs. Must call RecreatePhysics() after all)

true

boolean

auto_unflip (auto rotates the vehicle if flipped)

true

string

engine_sound_asset

NanosWorld::A_Vehicle_Engine_01

string

horn_sound_asset

NanosWorld::A_Vehicle_Horn_Toyota

string

brake_sound_asset

NanosWorld::A_Vehicle_Brake

string

engine_start_sound_asset

NanosWorld::A_Car_Engine_Start

string

vehicle_door_sound_asset

NanosWorld::A_Vehicle_Door

Note

Most of the functions below will reset the vehicle Physics State (automatically), which means the vehicle will stop immediately if moving.

Functions

Returns

Name

Description

AddStaticMeshAttached(
string id,
string static_mesh_path,
string socket = “”,
Vector relative_location = Vector(),
Rotator relative_rotation = Rotator()
)

Spawns and Attaches a StaticMesh into this Vehicle in a Socket with relative Location and Rotation. Uses a custom ID to be used for removing it further

server-only-label

Horn(boolean)

Starts or stops the vehicles horn

server-only-label

RecreatePhysics()

Recreate the Vehicle Physics (call this after configuring the vehicle if using AutoCreatePhysics = false)

RemoveStaticMeshAttached(string id)

Removes, if existing, a StaticMesh from this Vehicle given it’s custom ID

SetDefaultMaterial(
MaterialType material_type = MaterialType.Masked
)

Replaces all materials of this actor with a nanos basic material with comon parameters available (Masked, Translucent, TranslucentDepth)

server-only-label

SetDifferentialSetup(
DifferentialType differential_type = LimitedSlip_4W,
number front_rear_split = 0.45,
number front_left_right_split = 0.5,
number rear_left_right_split = 0.5,
number center_bias = 1.3,
number front_bias = 1.3,
number rear_bias = 1.3
)

Configures the Vehicle Differential. Check the page bottom to see each property explanation

server-only-label

SetEngineSetup(
number max_rpm = 4500,
number moi = 1,
number damping_rate_full_throttle = 0.15,
number d_r_zero_trt_clutch_engaged = 2,
number d_r_zero_trt_clutch_disengaged = 0.35
)

Configures the Vehicle Engine. Check the page bottom to see each property explanation

SetMaterialColorParameter(
string parameter_name,
Color color
)

Sets a Color parameter in this actor’s material

SetMaterialScalarParameter(
string parameter_name,
number scalar
)

Sets a scalar parameter in this actor’s material

SetMaterialTextureParameter(
string parameter_name,
string texture_path
)

Loads a image from computer (relative to Assets/ folder) and sets as parameter in this actor’s material

SetMaterialVectorParameter(
string parameter_name,
Vector vector
)

Sets a Vector parameter in this actor’s material

server-only-label

SetVehicleSetup(
number drag_coefficient = 0.3,
number throttle_input_rise_rate = 6,
number throttle_input_fall_rate = 10,
number brake_input_rise_rate = 6,
number brake_input_fall_rate = 10,
number handbrake_input_rise_rate = 12,
number handbrake_input_fall_rate = 12,
number steering_input_rise_rate = 2.5,
number steering_input_fall_rate = 5
)

Configures the Vehicle General Settings. Check the page bottom to see each property explanation

server-only-label

SetTransmissionSetup(
boolean has_automatic_transmission = true,
number gear_switch_time = 0.5,
number gear_auto_box_latency = 2,
number final_ratio = 4,
number clutch_strength = 10
)

Configures the Vehicle Transmission. Check the page bottom to see each property explanation

server-only-label

SetWheel(
number index (0-3),
string bone_name,
number shape_radius = 30,
number shape_width = 10,
number steer_angle = 70,
number mass = 20,
number damping_rate = 0.25,
number lat_stiff_max_load = 2,
number lat_stiff_value = 17,
number long_stiff_value = 1000,
number suspension_force_offset = 0,
number suspension_max_raise = 10,
number suspension_max_drop = 10,
number suspension_natural_frequency = 7,
number suspension_damping_ratio = 1,
number max_brake_torque = 1500,
number max_handbrake_torque = 3000,
boolean is_affected_by_handbrake = true,
Vector offset = Vector(0, 0, 0)
)

Configures a Vehicle Wheel. Check the page bottom to see each property explanation

server-only-label

SetDoor(
number seat_index,
Vector offset_location,
Vector seat_location,
Rotator seat_rotation,
number trigger_radius,
number leave_lateral_offset
)

Adds a Door at OffsetLocation from root which will pose the Character at SeatLocation with SeatRotation rotation. LeaveLateralOffset is where the Character will be ejected when leaving it (e.g. -150 for left door or 150 for right door)

server-only-label

SetSteeringWheelSetup(
Vector location,
number radius
)

Configures where the Steering Wheel is located, so Characters can grab it procedurally properly

server-only-label

SetHeadlightsSetup(
Vector location,
Color color = Color(1, 0.86, 0.5)
)

Configures the Headlights Offset and Color.

string

GetAssetName()

Gets the Asset name

Character

GetPassenger(number seat)

Gets all passengers. Returns nil if Seat is invalid or empty.

Character[]

GetPassengers()

Gets all passengers

Functions (Inherited from Actor)

Returns

Name

Description

authority-only-label

AddImpulse(
Vector force,
boolean velocity_change = false
)

Applies a Force in world coordinate on this Actor (the force is applied on Client side, by the (in most of cases) the closest player of this object)

authority-only-label

AttachTo(
Actor other,
string bone_name = “”,
Vector relative_location = Vector(),
Rotator relative_rotation = Rotator()
)

Attaches this Actor to any other Actor with a Relative Offset/Rotation

authority-only-label

Destroy()

Destroys this Actor

authority-only-label

Detach()

Detaches this Actor from AttachedTo Actor

authority-only-label

SetCollision(CollisionType)

Sets the actor’s collision Collision Types: Normal, Static Only, NoCollision

authority-only-label

SetGravityEnabled(boolean)

Sets the actor’s gravity enabled

authority-only-label

SetHighlightEnabled(
boolean,
number Index
)

Sets if this Actors is Highlighting at a specific Index color

authority-only-label

SetInitialLocation(Vector)

Sets the initial location

authority-only-label

SetLocation(Vector)

Sets the actor’s location in the game world

server-only-label

SetNetworkAuthority(
Player player = nil,
number time_ms = 0
)

Sets this actor’s new Player network authority. This player will be manually assigned to handle this actor physics and share it’s location with other clients. The authority assignment will still be overriden by the game automatically, to avoid that set a big time value so the player can keep longer with the authority and override the natural authority selection.

authority-only-label

SetRotation(Rotator)

Sets the actor’s rotation in the game world

authority-only-label

SetScale(Vector)

Sets the actor’s scale

SetValue(
string key,
any value,
boolean sync_on_clients
)

Sets a value in this entity, which can be accessed by any package (optionally sync on clients if called from server)

TranslateTo(
Vector location,
number speed
)

Smoothly moves this actor to a Location with a certain Speed

RotateTo(
Rotator rotation,
number speed
)

Smoothly rotates this actor to a Rotation with a certain Speed

boolean

IsGravityEnabled()

Gets the actor’s gravity enabled

boolean

IsInWater()

Gets if the actor is in Water

boolean

IsValid()

Returns if this entity is valid (i.e. wasn’t destroyed and points to a valid entity)

number

GetCollision()

Gets the current actor’s collision

number

GetID()

Gets the network ID of this entity (same in both client and server)

Vector

GetLocation()

Get the actor’s position in the game world

server-only-label

Vector

GetInitialLocation()

Gets the initial location

Rotator

GetRotation()

Gets the actor’s rotation in the game world

Vector

GetScale()

Gets the actor’s scale

string

GetType()

Returns the type of this Actor

any

GetValue(string key)

Gets a value given a key

client-only-label

Vector

GetVelocity()

Returns the current actor Velocity

Events

Name

Arguments

Description

Horn

Vehicle self,
boolean is_honking

When Vehicle honks

Hit

Vehicle self,
number intensity

When Vehicle hits something

Events (Inherited from Actor)

Name

Arguments

Description

Destroy

Entity self

Triggered when an Entity is Destroyed

Spawn

Entity self

Triggered when an Entity is Spawned/Created

ValueChange

Entity self,
string key,
any value

Triggered when an Entity has it’s value changed

Parameters Detailed

Parameter Name

Description

DifferentialType differential_type

(LimitedSlip_4W, LimitedSlip_FrontDrive, LimitedSlip_RearDrive, Open_4W, Open_FrontDrive, Open_RearDrive)

number front_rear_split

Ratio of torque split between front and rear (>0.5 means more to front, <0.5 means more to rear, works only with 4W type)

number front_left_right_split

Ratio of torque split between front-left and front-right (>0.5 means more to front-left, <0.5 means more to front-right, works only with 4W and LimitedSlip_FrontDrive)

number rear_left_right_split

Ratio of torque split between rear-left and rear-right (>0.5 means more to rear-left, <0.5 means more to rear-right, works only with 4W and LimitedSlip_RearDrive)

number center_bias

Maximum allowed ratio of average front wheel rotation speed and rear wheel rotation speeds (range: 1..inf, works only with LimitedSlip_4W)

number front_bias

Maximum allowed ratio of front-left and front-right wheel rotation speeds (range: 1..inf, works only with LimitedSlip_4W, LimitedSlip_FrontDrive)

number rear_bias

Maximum allowed ratio of rear-left and rear-right wheel rotation speeds (range: 1..inf, works only with LimitedSlip_4W, LimitedSlip_FrontDrive)

number max_rpm

Maximum revolutions per minute of the engine

number moi

Moment of inertia of the engine around the axis of rotation (Kgm^2)

number damping_rate_full_throttle

Damping rate of engine when full throttle is applied (Kgm^2/s)

number d_r_zero_trt_clutch_engaged

Damping rate of engine in at zero throttle when the clutch is engaged (Kgm^2/s)

number d_r_zero_trt_clutch_disengaged

Damping rate of engine in at zero throttle when the clutch is disengaged (in neutral gear) (Kgm^2/s)

number drag_coefficient

DragCoefficient of the vehicle chassis

number …input_rise_rate

Rate at which the input value rises

number …input_fall_rate

Rate at which the input value falls

number gear_switch_time

Time it takes to switch gears (seconds)

number gear_auto_box_latency

Minimum time it takes the automatic transmission to initiate a gear change (seconds)

number final_ratio

The final gear ratio multiplies the transmission gear ratios

number clutch_strength

Strength of clutch (Kgm^2/s)

number shape_radius

Radius of the wheel

number shape_width

Width of the wheel

number steer_angle

Steer angle in degrees for this wheel

number mass

Mass of this wheel

number damping_rate

Damping rate for this wheel (Kgm^2/s)

number lat_stiff_max_load

Max normalized tire load at which the tire can deliver no more lateral stiffness no matter how much extra load is applied to the tire

number lat_stiff_value

How much lateral stiffness to have given lateral slip

number long_stiff_value

How much longitudinal stiffness to have given longitudinal slip

number suspension_force_offset

Vertical offset from where suspension forces are applied (along Z-axis)

number suspension_max_raise

How far the wheel can go above the resting position

number suspension_max_drop

How far the wheel can drop below the resting position

number suspension_natural_frequency

Oscillation frequency of suspension. Standard cars have values between 5 and 10

number suspension_damping_ratio

The rate at which energy is dissipated from the spring. Standard cars have values between 0.8 and 1.2. Values < 1 are more sluggish, values > 1 or more twitchy

number max_brake_torque

Max brake torque for this wheel (Nm)

number max_handbrake_torque

Max handbrake brake torque for this wheel (Nm). A handbrake should have a stronger brake torque than the brake. This will be ignored for wheels that are not affected by the handbrake

Vector offset

If BoneName is specified, offset the wheel from the bone’s location. Otherwise this offsets the wheel from the vehicle’s origin