Car entering and exiting issue

I used the Car Enter\Exit script, and it works fine but I can only get out once. If I get back in, I can’t get out again. Also, as soon as I spawn, even though I am not in the trigger, I can press the key and enter the car. It’s odd. Here is the script:

// Attach this script to the door trigger gameobject.
// By default any driving/car/control component script in the inspector should start out at disabled
// so it will not move as the Player is being controlled by you, the user.
 
var car : Transform;
var player : Transform;
var exitPoint : Transform; // Place this empty gameobject next to the driver car door.
var doorTriggerLeft : Transform;
var PlayerCamera : Camera;
var CarCamera : Camera; // By default the camera "component" for the car camera should be set to OFF first.
var isPlayerVisible : boolean;
  
 
function Update()
{
if (Input.GetButtonDown("Car Enter Exit")&& isPlayerVisible) //Asign any key you want to enter/operate vehicle.
{
Debug.Log("Driving");
// make player invisible and still standing
player.gameObject.SetActiveRecursively(false);
player.gameObject.active = false;
// parent player to Exit Point
player.parent = exitPoint.transform;
player.transform.localPosition = Vector3(-1.5,0,0);
// parent PlayerParent to car
exitPoint.parent = car.transform;
 //Driverside exit point, adjust accordingly per vehicle.
// GameObject.Find("VehicleObjectName").GetComponent("DrivingScriptHere").enabled=true;
GameObject.Find("Car").GetComponent("Car").enabled=true; //Enables the script component to operate Vehicle.
PlayerCamera.enabled = false; //Disables the playerCamera
CarCamera.enabled = true; //Enables the carCamera
}
else
{
if (Input.GetButtonDown("Car Enter Exit")) //Asign any key you want to exit/park vehicle.
{
Debug.Log("Walking");
// make character visible again
player.gameObject.SetActiveRecursively(true);
player.gameObject.active = true;
// unparent player from everything
player.transform.parent = null;
// exitPoint.parent = doorTriggerLeft.transform;
// parent Exit Point to car gameobject
exitPoint.parent = car.transform;
// GameObject.Find("VehicleObjectName").GetComponent("DrivingScriptHere").enabled=false;
GameObject.Find("Car").GetComponent("Car").enabled=false; //Disables the script component to park Vehicle.
PlayerCamera.enabled = true; //re-enables player camera
CarCamera.enabled = false; //disables car camera
 
}
}
}
 
function OnTriggerEnter(Player : Collider)
{
Debug.Log("Trigger Enter");
isPlayerVisible = true;
}
 
function OnTriggerExit(Player : Collider)
{
Debug.Log("Trigger Exit");
isPlayerVisible = false;
}

I have ‘Car Enter Exit’ rigged to ‘F’, so if I press ‘F’ I either get in or out of the car. I also have the script attached to the doortriggerLeft. I can only get out of the car once, though. Once I get back in, I can’t exit again.

OMG I just figured it out! I had to put

isPlayerVisible

after

if (Input.GetButtonDown("Car Enter Exit")&& isPlayerVisible)
{
Debug.Log("Driving");

EDIT: Almost forgot, still the issue of the player being able to enter from anywhere (that is, until he enters and exits the trigger for the first time). Any help on that?

Easy answer man! :smiley: just got it today :smiley: i don’t have any clue why my player can enter a car from anywhere BUT here is the simple explanation:

function OnTriggerEnter(Player : Collider)
{
Debug.Log(“Trigger Enter”);
isPlayerVisible = true;
}


you have something like this! and you have to reedit to this:


function OnTriggerEnter(other : Collider){

if (other.gameObject.tag == “Player”){
isPlayerVisible = true;
}
}

and do the same with OnTriggerExit (: