Advanced Vehicle
TIP
It takes about 15 minutes to reading this article.
This article outlines the instructions of Advanced Vehicle logical objects in the editor.
What is Advanced Vehicle?
Advanced Vehicle is a logical object feature provided by the editor for quickly implementing a ground-based wheeled vehicle with simulated physical characteristics and for building vehicle appearances through models or effects.
How to use Advanced Vehicle
- step. 1 create an Advanced Vehicle from the local asset library Search for “Advanced Vehicle” in the local asset library, find the function object and drag it into the scene to complete the creation.
- Step.2 Build Vehicle Avatar Use any model or effect to assemble your Advanced Vehicle, and the model will automatically become part of the vehicle's collision.
TIP
- Pay attention to the mass of the carWhen assembling the body, the mass of the model object will be included in the overall vehicle mass. When assembling a model with too much mass, it is necessary to consider whether the vehicle center of gravity will affect the balance, otherwise, there will be a rollover.
Step.3 Bind Vehicle Wheels
Advanced Vehicle allows you to designate a model as the vehicle's wheel, and when tied, the model will have an animation of tire rotation. Wheel collisions for Advanced Vehicles need to be set by the tire radius in the properties, and the bound model has no collision effect. The Advanced Vehicle can bind 0 to 4 wheels or adjust the wheel position, but consider the influence of wheel position on the overall balance of the body.
Power wheel group properties | Description | Type | Value Range |
---|---|---|---|
Tire Radius | Set the radius of the physical tire | number | 0 - 1000 |
Steering Angle | Set the maximum tire steering angle, recommended interval (0 - 40). | number | 0 - 90 |
Bind Wheel | Designate an object as a tire for the vehicle | GameObject | Model or Effect |
- step. 4 Automatic boarding function Auto-boarding of Advanced Vehicles, accomplished by creating logical object with Triggers and Interactor that quickly verify vehicle operation when the editor runs.
Property | Description |
---|---|
Automatic boarding | When enabled, auto-executes driving logic when the character enters the vehicle's default trigger range. Once in the car, you can control vehicle movement by pressing W/A/S/D, the space bar controls vehicle braking, and the F key controls the character to get out of the car. Custom script logic under Advanced Vehicle objects cannot be executed while using Auto-boarding function. |
Hide Driver | Character will automatically hide their character appearance after executing the on-board logic. |
Offsetting position | Set the relative position of the character from the vehicle center point when the character leaves the vehicle. |
Vehicle Trigger | Auto-boarding logic can only be executed after binding the trigger under the Advanced Wheeled Vehicle object. |
Vehicle interactor | Auto-boarding logic can only be executed after binding an interactive object under the Advanced Wheeled Vehicle object. |
- step. 5 Run and experience the effects of the vehicle
Basic property | Description | Type | Value Range |
---|---|---|---|
Vehicle Mode | Select basic or advanced mode to set vehicle properties. | enum | Base Mode |
Mass | The vehicle's basic mass will affect driving speed and stability. When adding additional object mosaic vehicle profiles, the mass of the object is added to the vehicle's total mass and the vehicle's physical center of gravity is reallocated. | number | 0.01 - 100000(KG) |
Friction | Set the tire friction of the vehicle, which will affect the operation effect of the vehicle. When the friction is 0, the tire and the ground friction fail, which will cause the vehicle to be unable to move. | number | 0 - 8 |
Revolutions per minute (RPM) | Set the maximum engine speed per minute. The higher the speed, the greater the maximum speed the vehicle can achieve. (Note that this is the vehicle engine speed and does not represent the KM/H speed during actual operation.) | number | 0 - 1000000 |
Acceleration Coefficient | Set vehicle acceleration factor. The acceleration effect is based on the physical simulation operation, the direct modification of the acceleration coefficient will not cause a large effect gap, and the vehicle driving speed can be further adjusted with other parameters such as the friction force and mass of the vehicle. | number | 0.1 - 100 |
Brake Speed | Set the braking force for active braking. | number | 0 - 1000000 |
An advanced wheeled vehicle can designate a model as the vehicle's tire, and when tied, the model will have an animation of tire rotation. Tire collisions for Advanced Vehicles need to be set by the tire radius in the properties, and the bound model has no collision effect.
Power wheel group properties | Description | Type | Value Range |
---|---|---|---|
Tire Radius | Set the radius of the physical tire | number | 0 - 1000 |
Steering Angle | Set the maximum tire steering angle, recommended interval (0 - 40). | number | 0 - 90 |
Bind Wheel | Designate an object as a tire for the vehicle | GameObject | Model or Effect |
After selecting Advanced Mode, you can customize engine parameters to make vehicles with different power. Engine attributes simulate real-world physical characteristics,and the corresponding vehicle power is calculated from the gear ratio.
Advanced Mode Property | Description | Type | Value Range |
---|---|---|---|
Low gear ratio | The speed ratio of each gear when automatically downshifting. | number | 0 - 1 |
High gear ratio | The speed ratio of each gear at automatic upshift. | number | 0 - 1 |
How to script Advanced Vehicles
- step. 6 create a vehicle control script
@Component
export default class NewScript extends Script {
vehicle: mw.AdvancedVehicle;
interactiveObj: mw.Interactor;
trigger: mw.Trigger;
/** When the script is instanced, this function is called before the first frame update */
protected onStart(): void {
this.vehicle = mw.GameObject.findGameObjectById("337D3F6F") as mw.AdvancedVehicle; //Gets an Advanced Vehicle logical object
this.interactiveObj = mw.GameObject.findGameObjectById("2093ED53") as mw.Interactor; // Gets an interactor logical object
this.trigger = mw.GameObject.findGameObjectById("09523175") as mw.Trigger; // Get trigger logic object
// Activate the boarding event through the trigger.
if (SystemUtil.isClient()) {
this.trigger.onEnter.add((chara: mw.Character) => {
if (chara instanceof mw.Character) {
Event.dispatchToLocal("InVehicle");
Event.dispatchToServer("setPlayerCollision",false);
}
}
// Client executes loading logic
Event.addLocalListener("InVehicle", () => {
let player = Player.localPlayer;
player.character.setCollision(mw.PropertyStatus.Off) // Turn off character collision to avoid collision between character and vehicle
Camera.currentCamera.springArm.collisionEnabled = false; // Turn off camera collision to avoid collision between camera and vehicle
this.interactiveObj.enter(player.character, mw.HumanoidSlotType.Buttocks, "14015"); //Activate the interactive object
this.vehicle.owner = player;
this.VehicleKeyEvents();
}
// Client executes the exit logic
Event.addLocalListener("LeaveVehicle", () => {
let player = Player.localPlayer;
let endLoc = this.trigger.worldTransform.position; //Gets the current trigger position as the exit position
this.interactiveObj.leave(endLoc.add(new mw.Vector(0, 200, 50)); // activate the interactive and set a drop-off position
this.vehicle.owner = null; // Clear vehicle control
Player.character.setCollision(mw.PropertyStatus.On); // Open Character Collision
}
// character collision is processed on the server
Event.addClientListener("setPlayerCollision",(player,value:boolean)=>{
if(value){
player.character.setCollision(mw.PropertyStatus.On) // Open Character Collision
}else{
Player.character.setCollision(mw.PropertyStatus.Off) // Character Collision OFF
}
}
}
/**
* Push the button to control vehicle movement
*/
private VehicleKeyEvents() {
// Press the UP button to move the vehicle forward.
InputUtil.onKeyDown(mw.Keys.Up, () => {
this.vehicle.throttleInput = 1;
}
InputUtil.onKeyUp(mw.Keys.Up, () => {
this.vehicle.throttleInput = 0;
}
// Press the Down button to slow down the vehicle.
InputUtil.onKeyDown(mw.Keys.Down, () => {
this.vehicle.throttleInput = -1;
}
InputUtil.onKeyUp(mw.Keys.Down, () => {
this.vehicle.throttleInput = 0;
}
// Press the left button and turn the vehicle steering wheel to the left.
InputUtil.onKeyDown(mw.Keys.Left, () => {
this.vehicle.steeringInput = -1;
}
InputUtil.onKeyUp(mw.Keys.Left, () => {
this.vehicle.steeringInput = 0;
}
// Press the right button and turn the vehicle steering wheel to the right.
InputUtil.onKeyDown(mw.Keys.Right, () => {
this.vehicle.steeringInput = 1;
}
InputUtil.onKeyUp(mw.Keys.Right, () => {
this.vehicle.steeringInput = 0;
}
// Press space bar, vehicle brake
InputUtil.onKeyDown(mw.Keys.SpaceBar, () => {
this.vehicle.handbrakeInputEnable = true;
}
InputUtil.onKeyUp(mw.Keys.SpaceBar, () => {
this.vehicle.handbrakeInputEnable = false;
}
// Press F to get out of the car;
InputUtil.onKeyDown(mw.Keys.F, () => {
Event.dispatchToLocal("LeaveVehicle");
Event.dispatchToServer("setPlayerCollision",true);
}
}
}
@Component
export default class NewScript extends Script {
vehicle: mw.AdvancedVehicle;
interactiveObj: mw.Interactor;
trigger: mw.Trigger;
/** When the script is instanced, this function is called before the first frame update */
protected onStart(): void {
this.vehicle = mw.GameObject.findGameObjectById("337D3F6F") as mw.AdvancedVehicle; //Gets an Advanced Vehicle logical object
this.interactiveObj = mw.GameObject.findGameObjectById("2093ED53") as mw.Interactor; // Gets an interactor logical object
this.trigger = mw.GameObject.findGameObjectById("09523175") as mw.Trigger; // Get trigger logic object
// Activate the boarding event through the trigger.
if (SystemUtil.isClient()) {
this.trigger.onEnter.add((chara: mw.Character) => {
if (chara instanceof mw.Character) {
Event.dispatchToLocal("InVehicle");
Event.dispatchToServer("setPlayerCollision",false);
}
}
// Client executes loading logic
Event.addLocalListener("InVehicle", () => {
let player = Player.localPlayer;
player.character.setCollision(mw.PropertyStatus.Off) // Turn off character collision to avoid collision between character and vehicle
Camera.currentCamera.springArm.collisionEnabled = false; // Turn off camera collision to avoid collision between camera and vehicle
this.interactiveObj.enter(player.character, mw.HumanoidSlotType.Buttocks, "14015"); //Activate the interactive object
this.vehicle.owner = player;
this.VehicleKeyEvents();
}
// Client executes the exit logic
Event.addLocalListener("LeaveVehicle", () => {
let player = Player.localPlayer;
let endLoc = this.trigger.worldTransform.position; //Gets the current trigger position as the exit position
this.interactiveObj.leave(endLoc.add(new mw.Vector(0, 200, 50)); // activate the interactive and set a drop-off position
this.vehicle.owner = null; // Clear vehicle control
Player.character.setCollision(mw.PropertyStatus.On); // Open Character Collision
}
// character collision is processed on the server
Event.addClientListener("setPlayerCollision",(player,value:boolean)=>{
if(value){
player.character.setCollision(mw.PropertyStatus.On) // Open Character Collision
}else{
Player.character.setCollision(mw.PropertyStatus.Off) // Character Collision OFF
}
}
}
/**
* Push the button to control vehicle movement
*/
private VehicleKeyEvents() {
// Press the UP button to move the vehicle forward.
InputUtil.onKeyDown(mw.Keys.Up, () => {
this.vehicle.throttleInput = 1;
}
InputUtil.onKeyUp(mw.Keys.Up, () => {
this.vehicle.throttleInput = 0;
}
// Press the Down button to slow down the vehicle.
InputUtil.onKeyDown(mw.Keys.Down, () => {
this.vehicle.throttleInput = -1;
}
InputUtil.onKeyUp(mw.Keys.Down, () => {
this.vehicle.throttleInput = 0;
}
// Press the left button and turn the vehicle steering wheel to the left.
InputUtil.onKeyDown(mw.Keys.Left, () => {
this.vehicle.steeringInput = -1;
}
InputUtil.onKeyUp(mw.Keys.Left, () => {
this.vehicle.steeringInput = 0;
}
// Press the right button and turn the vehicle steering wheel to the right.
InputUtil.onKeyDown(mw.Keys.Right, () => {
this.vehicle.steeringInput = 1;
}
InputUtil.onKeyUp(mw.Keys.Right, () => {
this.vehicle.steeringInput = 0;
}
// Press space bar, vehicle brake
InputUtil.onKeyDown(mw.Keys.SpaceBar, () => {
this.vehicle.handbrakeInputEnable = true;
}
InputUtil.onKeyUp(mw.Keys.SpaceBar, () => {
this.vehicle.handbrakeInputEnable = false;
}
// Press F to get out of the car;
InputUtil.onKeyDown(mw.Keys.F, () => {
Event.dispatchToLocal("LeaveVehicle");
Event.dispatchToServer("setPlayerCollision",true);
}
}
}