Collisions Detection. Multiple Controllers

Okay so I have looked everywhere for the answer to my problem but found nothing, so I am guessing that it is something simple so I would like the community to look over my script for me.
Basically I am trying to make a GTA style game where the character (A First Person Controller) can collide with the car (Copy and Paste from the Car tutorial). The Character then presses “E” ad the debug log tells me what happened.
This is what I have so far.

var playerCamera : Camera;
var enterCar:boolean = false;

function OnCollisionEnter(collision : Collision){
if (collision.gameObject.tag == "vehicleCollision");
enterCar = true;
}

function OnCollisionExit(collision : Collision){
enterCar = false;
}

function Start(){
if(enterCar == false);
var vehicle = GameObject.FindGameObjectWithTag("Car");
	yield WaitForSeconds (2);
	print(Time.time);
	vehicle.GetComponent(Car).enabled = false;
}

function Update(){

		if (Input.GetButtonDown("Enter") && (enterCar == true))
		var vehicle = GameObject.Find("Vehicle");
		vehicle.GetComponent(Car).enabled = true;
		playerCamera.enabled = false;
		print("is in the car");
}

1 Answer

1

You said when press “E” key, but your code is checking for “Enter” key. But you probably have a exception in your Update function: if get Enter pressed and entercar is true, then you create the var vehicle. If don’t get Enter or entercar is false, you try to use the var vehicle, but this var was not created, because the if statement was ignored.
You should do:

if (Input.GetButtonDown("Enter") && (enterCar == true))
{
       var vehicle = GameObject.Find("Vehicle");
       vehicle.GetComponent(Car).enabled = true;
       playerCamera.enabled = false;
       print("is in the car");
}