Hi all. I am trying to build a simple system for entering and exiting vehicles. Currently both the player and the vehicle are the default FPS controllers. I am able to run up to the vehicle controller, enter it, disable the player controller, and drive the vehicle. However, I cannot re-enable the disabled player controller. Any and all help is appreciated. Here is the code I am using, it is applied to the vehicle, which has a trigger for range sensing.
Here is my code
#pragma strict
var inRange = false;
var inCar = false;
//This is the player that enters the vehicle:
var player : Transform;
//This is the player camera:
var playerCamera : Camera;
//This is the Vehicle:
var Vehicle : Transform;
//This is the vehicle camera:
var vehicleCamera : Camera;
//These are the control scripts for your vehicle:
private var lookScript : MouseLook;
private var moveScript : CharacterMotor;
private var inputScript : FPSInputController;
function Start () {
playerCamera.enabled = true;
vehicleCamera.enabled = false;
//These are the vehicle control scripts.
lookScript = GetComponent(MouseLook);
moveScript = GetComponent(CharacterMotor);
inputScript = GetComponent(FPSInputController);
}
function Update () {
if(Input.GetButton("Enter") && inRange == true) {
if(inCar == false) {
inCar = true;
//Switch Cameras
playerCamera.enabled = false;
vehicleCamera.enabled = true;
//Parent player to Vehicle
player.parent = Vehicle.transform;
player.transform.localPosition = Vector3(-1.5,0,0);
//Make Player Invisible
player.gameObject.SetActive(false);
//Enable Vehicle Control Scripts
lookScript.enabled = !lookScript.enabled;
moveScript.enabled = !moveScript.enabled;
inputScript.enabled = !inputScript.enabled;
}
}
else if(Input.GetButton("Enter") && inCar == true) {
//inCar = false;
//Switch Cameras
playerCamera.enabled = true;
vehicleCamera.enabled = false;
//Unparent player to Vehicle
player.transform.parent = null;
//Make Player Visible
player.gameObject.SetActive(true);
//Enable Vehicle Control Scripts
lookScript.enabled = !lookScript.enabled;
moveScript.enabled = !moveScript.enabled;
inputScript.enabled = !inputScript.enabled;
}
}
function OnTriggerEnter (other : Collider) {
inRange = true;
Debug.Log("InRange!");
}
function OnTriggerExit (other : Collider) {
inRange = false;
}
If anyone can help me figure this out, that would be amazing. Thank you in advance.