how do i go about making a script that will allow me to enter and exit a vehicle by pressing E on my keyboard.
Hi,
I was doing this a while ago, but am unable to post my code as it is on a computer inside the cordoned area in Christchurch (New Zealand). If you are unable to work something out by the time I can retreive my stuff I'll post the code but in the meantime here are the main things I had to think about:
are there going to be multiple "seats" in the vehicle?
if so, can you jump from seat to seat WITHOUT geting out of the vehicle?
how far from the "entry point" do you have to be when you press "e"
is the vehicle going to have its own cameras or are you going to use the player cam?
are you going to have a different camera for each "seat"?
is there an animation for getting in and out of the vehicle?
I used two arrays, one for entry point and one for camera. With my script you get a distance from the vehicle and push "e_key" and :
the player object is parented to the seat and local transform set to 0,0,0
player scripts are deactivated (eg fps turned off)
vehicle scripts are activated
player camera is deactivated and appropiate camera is activated
you push number keys and you switch "seat" (drive, observe, gun etc)
Start with parenting on "e" and then work on the cameras. If you do use multiple cameras remember to disable the ones on the vehicle as soon as the game starts - otherwise you will have one camera being dominant and it will look like you are jumping to the wrong vehicle if you have a few of them.
Sorry I cannot be more helpfull at this stage but this should get you started!
OK so I've got most of my stuff but the most recent version of the script has been lost. You should be able to do something with this one.
// arrays
var myCams : Camera[];
var mySeats : Transform[];
// timer
var timer : int = 3;
// ship
var ship : GameObject;
// active control vars
private var state : int = -1;
private var count : int = 0;
// player position & cam
var player_object : Transform;
var player_camera : Camera;
var old_position : Vector3;
function Update()
{
if (Input.GetAxis ("e_key"))
{
if (state == -1 && timer < 0)
{
for (var index : int = 0; index < mySeats.Length; index++)
{
if(Vector3.Distance (mySeats[index].position, player_object.position ) < 3 )
{
// deactivate first person camera
player_camera.enabled = false;
// activate other camera
myCams[index].enabled = true;
// now sitting at
state = index;
print (index);
// tell turret / driving scripts they can activate
BroadcastMessage ("Activate_Turret", state);
// reset timer
timer = 20;
// parent user to seat
player_object.parent = mySeats[index];
old_position = player_object.transform.localPosition;
player_object.transform.localPosition = Vector3(0, 0, 0);
// break out of the for loop cause we have already selected one seat
break;
}
}
}
// get out
if (state > -1 && timer < 0)
{
// we are now first person again
state = -1;
// reset timer
timer = 30;
// tell turret / driving scripts they can deactivate
BroadcastMessage ("Activate_Turret", -1);
// put back relitave to where the player entered the vehicle/seat
player_object.transform.localPosition = old_position;
// Detaches the transform from its parent.
transform.parent = null;
// reactivare first person camera
player_camera.GetComponent("Camera").enabled = true;
// disable all other cameras
for (var thisCam in myCams)
{
thisCam.GetComponent("Camera").enabled = false;
}
}
}
// fix speed
timer = timer - 1;
}