Character Enter/Exit
Last updated
Last updated
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.
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.
Add a Vehicle component
Add a Rigidbody component (may be kinematic or not)
Add a Vehicle Enter Exit Manager component
Make sure the character has a collider of some kind (may be a Character Controller)
Next, set up the vehicle:
Make sure it has a Vehicle component
Make sure it has a Rigidbody component (kinematic or not)
Add a Vehicle Enter Exit Manager component
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.
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 when she ship is not landed.
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.
To rebind the controls, go to the project tab and double click the General Input Asset to open up the Input System editor. By default, the control used for entering and exiting is the General Controls > Use control.
To set up a text prompt for entering/exiting:
Set up a text component somewhere in the scene or as part of the character's HUD (can be UGUI or TMPro).
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.
Drag the text object into the 'Prompt Text' field of 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.
To customize the prompts:
Go to the PlayerInput_InputSystem_EnterExitControls component you set up earlier.
Customize the enter/exit prompts.
To make sure that the prompts always refer to the control bindings you have set up, be sure to refer to the controls with {control}. Anywhere this appears in a prompt, it will be replaced at runtime with the correct control reference.
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.
If the player starts off in a vehicle other than the character, but you still want them to be able to exit to the character during gameplay, make sure to go to whatever vehicle the player starts off in, go to its Vehicle Enter Exit Manager component, and set Starting Child Vehicle to the character.
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.