- Home /
Entering and Exiting Vehicals
Now I know that this question gets asked a lot, but my script simply refuses to work properly. I have this script through some tuts, but it won't work like it will in those. Here is my script, I hope that somebody can help me out. Thanks!
// 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"))
{
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;
}
Answer by Bunny83 · Mar 31, 2012 at 10:38 AM
You usually want to use Input.GetButtonDown instead of Input.GetAxis. That way you don't need this hacky timer which doesn't even work very stable in your case. You set the timer to 20 and you subtract 1 each frame. At a quite high framerate it will toggle very fast while you hold down "e".
Input.GetButtonDown is only true in one frame.
You should be more precise when you ask a question.
"but it won't work like it will in those" doesn't explain your problem in any way.
Answer by Major · Apr 01, 2012 at 12:05 AM
Okay you have a good point. And what isn't working is I can go up to the car, click e and i'll end up in the car. But I am facing the wrong direction, I can't move, and the FPS controller is still active.
Your answer
Follow this Question
Related Questions
could someone help with entering/exiting a vehicle? 0 Answers
Enter/Exit vehicle 0 Answers
Car Enter/Exit Script Multiple Cars Problem 4 Answers
Problem Enter and Exit with Vehicle 2 Answers