[Help] Script to enter and exit a vehicle

Hello, i’ve been trying to write my own script for entering and exitting a vehicle, but i’ve encountered a problem that i haven’t been able to solve myself, so i was hoping i could get some help here :slight_smile:

Everything, at the moment, seems to be working perfectly as i wanted it to, except for when i’m trying to exit the vehicle. I just can’t figure out how to do that :frowning:

#pragma strict

var _enableVehicleEnter = false;

var isControllingPlayer = true;
var isControllingBoatVehicle = false;

//Transforms
var cam : Transform;
var player : Transform;
var boatVehicle : Transform;


function Update () 
{
	//Defining the target of my MouseOrbit Script
	var MouseOrbitScript : MouseOrbit = GameObject.Find("Camera").GetComponent("MouseOrbit");
			
	//Defining the control script for the boat
	var BoatScript : BoatScript = GameObject.Find("Boat").GetComponent("BoatScript");

	//Defining the control script for the player
	var PlayerMovementScript : PlayerMovement = GameObject.Find("Teddy").GetComponent("PlayerMovement");
	
	
		//If we are controlling a vehicle, we can't be controlling the player
		if (isControllingBoatVehicle == true)
		{
			isControllingPlayer = false;

			MouseOrbitScript.target = boatVehicle;
			MouseOrbitScript.distance = 50;
			
			BoatScript.enabled = true;
			PlayerMovementScript.enabled = false;
		}
		else
		{
			isControllingBoatVehicle = false;
			MouseOrbitScript.distance = 20;
			
			MouseOrbitScript.target = player;
			BoatScript.enabled = false;
			PlayerMovementScript.enabled = true;
		}	
			
		
		//Enters the boat if button is pushed and if we are inside the collider
		if (_enableVehicleEnter == true)
		{
			if (Input.GetButton("EnterVehicle"))
				isControllingBoatVehicle = true;
		}
		
		else if (isControllingBoatVehicle == true)
		{
			if (Input.GetButton("EnterVehicle"))
				isControllingBoatVehicle = false;
		}
}


//Enable us to enter vehicle if we are in range
function OnTriggerEnter (EnableCol : Collider)
{
	_enableVehicleEnter = true;
}
function OnTriggerExit (DisableCol : Collider)
{
	_enableVehicleEnter = false;
}

OnTriggerEnter and OnTriggerExit should refer to a separate vehicle.

Take both of them and add them to a new script and put it on a vehicle as such:

//Enable us to enter vehicle if we are in range
function OnTriggerEnter (EnableCol : Collider)
{
	var script:WhateverScript = EnableCol.gameObject.GetComponent("WhateverScript");
	if(script){
		script._enableVehicleEnter = true;
		script.boatVehicle = transform;
	}
}

function OnTriggerExit (DisableCol : Collider)
{
    var script:WhateverScript = DisableCol.gameObject.GetComponent("WhateverScript");
	if(script){
		script._enableVehicleEnter = false;
		if(script.boatVehicle == transform)script.boatVehicle = null;
	}
}

Also, some changes:

Predefine the MouseOrbit. Dont search for it every frame. (move that search to the Start function)
Player should be this game object, so the search for the “Teddy” script should look like this:

var PlayerMovementScript : PlayerMovement = gameObject.GetComponent(“PlayerMovement”);

Now, with these changes, you should be able to walk up to one of several boats, and press the “EnterVehicle” key and it will simply use the boat that you last walked into its trigger for.

All you will then have to do is make the BoatScript control whatever boat that is linked in this script (or set it when you enter the boat)

I tried having the definition of “MouseOrbit”, “Boat Control Script” and the “Player Movement Script” in the start function, but it won’t work. I get the following error

for every time i try to use the “MouseOrbitScript” :confused:

EDIT

I made another script for the “function OnTriggerEnter” and “function OnTriggerExit”. Now i have to problems with this.

1: When i a nullReferenceException from line 10, when i enter my boat vehicle.

and another from line 21 when i move the boat so that my player is outside of it’s collider, then i keep controlling the boat, but the camera sort of freezes in place.

#pragma strict

//Enable us to enter vehicle if we are in range
function OnTriggerEnter (EnableCol : Collider)
{	
	var EnterVehicle : EnterVehicleScript = EnableCol.gameObject.GetComponent("EnterVehicleScript");
	
	if (BoatScript)
	{
		EnterVehicle._enableVehicleEnter = true;
		EnterVehicle.boatVehicle = transform;
	}
}

function OnTriggerExit (DisableCol : Collider)
{
	var EnterVehicle : EnterVehicleScript = DisableCol.gameObject.GetComponent("EnterVehicleScript");
	
	if (BoatScript)
	{
		EnterVehicle._enableVehicleEnter = false;
		if (EnterVehicle.boatVehicle == transform)
			EnterVehicle.boatVehicle = null;
	}
}

note: I still haven’t been able to fix the problem with not being able to exit my vehicle with my “EnterVehicle”-button
note: I figured out how to get rid of the written errors, but the error that causes the camera to freeze is still there.