Character Enter/Exit

During the game, you may have a character that you wish to enter or exit a vehicle with by physically approaching and interacting with the vehicle. The following sections show you how to do this.
To enter/exit a vehicle without physically interacting with it (in code) see this section.

Setting Up The Character

To begin, set up your character as a vehicle. This might sound strange, but a character is simply another representation for what the player is controlling.
  1. 1.
    Add a Vehicle component
  2. 2.
    Add a Rigidbody component (may be kinematic or not)
  3. 3.
    Add a Vehicle Enter Exit Manager component
  4. 4.
    Make sure the character has a collider of some kind (may be a Character Controller)

Setting Up The Vehicle

Next, set up the vehicle:
  1. 1.
    Make sure it has a Vehicle component
  2. 2.
    Make sure it has a Rigidbody component (kinematic or not)
  3. 3.
    Add a Vehicle Enter Exit Manager component
  4. 4.
    Add a trigger collider, positioned where the character must be to enter the vehicle, and drag it into the Enter Exit Collider field in the inspector of the Vehicle Enter Exit Manager component.
  5. 5.
    Add a new game object somewhere in the vehicle's hierarchy to represent where the character should be spawned when exiting the vehicle, and drag it into the Spawn Point variable in the inspector of the Vehicle Enter Exit Manager component.
Note that for spaceships, you can use the Ship Enter Exit Manager component instead of the Vehicle Enter Exit Manager component, as it comes with functionality for preventing enter/exit unless the ship has landed.

Setting Up The Game Agent

Add a Player Input_Input System_Enter Exit Controls component somewhere in the hierarchy of the Game Agent you want to be able to switch vehicles.

Putting It Together

Now, to be able to enter a vehicle, the Game Agent must be controlling the character vehicle you just set up, so drag the character vehicle into the Starting Vehicle field in the inspector of the Game Agent component.
Now run the scene.
When the character enters the trigger collider you set up on the vehicle, you can enter the vehicle by pressing the relevant input (by default, the F key).
If you want the player to start off in the vehicle (not the character) but to be able to exit to the character, drag the character vehicle into the Starting Child Vehicle field in the inspector of the Vehicle Enter Exit Manager component on the vehicle.

Camera Control

If you're using a third party character controller (or your own) you probably want to switch off the character's camera controller when the character enters the vehicle.
You can easily do this by going to the character's Vehicle Enter Exit Manager and using the 'On Entered Parent' event to call a function to disable your camera controller, and the 'On Exited Parent' event to enable it again.
If you don't want to use the camera system provided in the kit for following your character, be sure not to add a Camera Target to the character, and the vehicle camera will simply do nothing and become passive when the player switches to the character.

Enter/Exit Prompts

To set up a text prompt for entering/exiting:
  1. 1.
    Set up a text component somewhere in the scene or in the vehicle's HUD (can be UGUI or TMPro)
  2. 2.
    If you're using TMPro, add a UVCText_TMProUGUI component to the text object OR if you're using UGUI, add a UVCText_UGUI component to the text object.
  3. 3.
    Drag the text object into the 'Prompt Text' field of the Vehicle Enter Exit Manager components on both the character and the vehicle.
  4. 4.
    Set the prompt values on the Vehicle Enter Exit Manager components on both the character and the vehicle.
Now you should get a prompt when you can enter or exit a vehicle.
Last modified 1yr ago