Improbable Icon

Vehicles

unity

#1

Hi,

How would you spawn in a vehicle and create an entity template for it? Should you use specificClient or PhysicsOnly on the VehicleInput component in the entity template? I’ve done it like this:

Summary

public static Entity CreateTestCarTemplate(string clientId) {

        var testCarTemplate = EntityBuilder.Begin()
            .AddPositionComponent(new Vector3(0, 2, 0), CommonRequirementSets.PhysicsOnly)
            .AddMetadataComponent(entityType: SimulationSettings.TestCarPrefabName)
            .SetPersistence(true)
            .SetReadAcl(CommonRequirementSets.PhysicsOrVisual)
            .AddComponent(new Rotation.Data(Quaternion.identity.ToNativeQuaternion()), CommonRequirementSets.PhysicsOnly)
            .AddComponent(new VehicleInput.Data(new Controller(motorTorque: 0, steerAngle: 0)), CommonRequirementSets.SpecificClientOnly(clientId)) 
            .Build();

        return testCarTemplate;
    }

Is this correct?

And lastly is there something specific that needs to be done when a player is entering and leaving the vehicle like delegating authority of the entity?

Just ask if you need clarification!

Thanks!


#3

HI @ChunkTreasure

It really depends on how you design your game.

Scenario 1 This vehicle is the only entity player able to control like the pirate ship in the Unity Pirate tutorial

In this case, you can set the specificClient on Vehicle Input templet as the player will provide input for the Vehicle.

Scenario 2 A player controlled game object able to take on and off the vehicle. The player gains the control of the Game Object while taking on the vehicle and loss the control when taking off.

I would say on a high level:

  • Add a “DriverData” component to the car (authority set to: ManagedWorker)
  • When a player presses “enter car” button: send a command to the DriverData component
  • When the Car receives the command: set a Driver variable on DriverData to the player’s entityID
  • In the Car’s update on the ManagedWorker: if it has a driver, use its Input-component’s data to move the car. You can get the input component from the driver either by finding the driver’s game-object or by using SpatialOS.View to find its SpatialOS Entity. (link to docs)

LocalEntities.Instance.Get(entityId).UnderlyingGameObject Game Object
Entity shipEntity = SpatialOS.Dispatcher.Entities[shipEntityId]; Spatial Entity

  • In the Player’s update on the ManagedWorker: this is a bit tricky, but if there is a car nearby that has this player as its driver, move the player’s position along with the car. You’ll have to do a Unity find to get all the cars and see if anyone is driven by you (the alternative is to have the car send a command to the player, but this will open your code up to tricky race-conditions).
  • In the Player’s update on the ClientWorker: same as above - if there is a car nearby that has this player as a driver, move the player’s GameObject to be exactly inside the car (so ignore the SpatialOS position). You have to do this on the ClientWorker to prevent the player from “lagging” behind the car. (edited)
    Hope this could help and let me know if you have further questions on this.