- Home /
Vehicle Tutorial
Does anybody know of a tutorial that can teach me how to make it so characters can enter vehicles and drive them around?
Answer by RetepTrun · Jun 01, 2011 at 05:16 PM
No tutorial but its not hard
In both the vehicle's and the man's script add an if and boolean so if it's true then the arrow keys make the car move, otherwise make the keys controll the man. Also parent the player to the car, while simutaneously moving the player to the position of a sear object.
You can look at this for clues, mine is a bit complicated because my game is split screen and the players are rigibodies. This script is attached to the car object which has a large trigger-box-collider, when we are within this trigger a person can press "p" and is teleported to the driver's seat.
Variables:
seats1&2-empty children of the car that just mark where to teleport the player
seat1exit: where to put player when he presses "u" to get out
camerA,B: cameras for player1or2 attached to the car that are turned on when we get in
personcamAB: the script finds the man's camera and turns it off when he gets in
bodycol 1 2 3: colliders of the car that we dont want the player inside to collide with because a rigidbody in a rigidbody makes things wierd
itsp1 p2: because I have 2 players my script identifies the different people by the type of movement script they have.
2 important lines:
GetComponent(car1).ControllableA = true; // here this script talks to the one with the car's controlls and says to let car be controlled
la.nut(); //la is the script of the player, and we call function nut() on it which makes it so the man cannot be controlled
<pre>
var seat1 : Transform;
var seat2 : Transform;
var seat1exit : Transform;
var seat2exit : Transform;
private var seat1used : boolean;
private var seat2used : boolean;
private var passenger : Transform;
private var passenger2 : Transform;
var personCamA:Camera;
var personCamB:Camera;
var cameraA: Camera;
var cameraB:Camera;
private var itsP1 :boolean=false;
private var itsP2 :boolean=false;
var bodycol1 : Transform;
var bodycol2: Transform;
var bodycol3: Transform;
function Start()
{
seat1used = false;
seat2used = false;
//GetComponent(MyScript).enabled = false;
cameraA = transform.Find("CameraA").camera;
cameraB = transform.Find("CameraB").camera;
//cameraA = transform.Find("CameraA");
//cameraB = transform.Find("CameraB");
cameraB.enabled = false;
cameraA.enabled = false;
}
function OnTriggerStay(other : Collider)
{
//first seat get in
if(other.tag == "Player" && other.rigidbody && Input.GetKey("p") && seat1used == false)
{
seat1used = true;
other.rigidbody.isKinematic = true;
passenger = other.transform;
passenger.parent = transform;
passenger.transform.position = seat1.position;
passenger.transform.rotation = seat1.rotation;
var la = passenger.GetComponent(physContrlr); //p1 seat1
var le = passenger.GetComponent(myControllerP2); //p2 seat1
if( passenger.GetComponent(physContrlr) )//for player1 1st seat controlls and cameras
{
personCamA = passenger.Find("Camera_P1").camera;
GetComponent(car1).ControllableA = true;
la.nut();
cameraA.enabled = true;
personCamA.enabled = false;
itsP1=true;
Physics.IgnoreCollision(passenger.collider, bodycol1.collider);
Physics.IgnoreCollision(passenger.collider, bodycol2.collider);
Physics.IgnoreCollision(passenger.collider, bodycol3.collider);
//cameraA.active = true;
}
if( passenger.GetComponent(myControllerP2))//for player2 1st seat controlls and cameras
{
personCamB = passenger.Find("Camera_P2").camera;
GetComponent(car1).ControllableB = true;
le.nut();
cameraB.enabled = true;
personCamB.enabled = false;
itsP2 = true;
//cameraB.active = true;
}
//p1 script = la
//p2 script = le
}
//second seat get in
if (other.tag == "Player" && other.rigidbody && Input.GetKey("y") && seat1used == true && seat2used == false)
{
passenger2 = other.transform;
seat2used = true;
other.rigidbody.isKinematic = true;
passenger2 = other.transform;
passenger2.parent = transform;
passenger2.transform.position = seat2.position;
passenger2.transform.rotation = seat2.rotation;
}
}
//
//
//
//}
//function Update()
//{
//if(Input.GetKey("p") && seat1used == false)
//{
//}
function Update(){
if (Input.GetKey("u") && seat1used == true)
{
passenger.transform.position = seat1exit.position;
passenger.transform.rotation = seat1exit.rotation;
passenger.parent = null;
passenger.rigidbody.isKinematic = false;
seat1used = false;
if(itsP1 == true){//player1 get out of 1st seat
passenger.GetComponent(physContrlr).nut();
GetComponent(car1).ControllableA = false;
cameraA.enabled = false;
personCamA.enabled = true;
}
if(itsP2 == true){
GetComponent(car1).ControllableB = false;
passenger.GetComponent(myControllerP2).nut2();
cameraB.enabled = false;
personCamB.enabled = true;
}
}
//get out of 2nd seat
if (seat2used == true && Input.GetKey("t"))
{
var da = passenger2.GetComponent(physContrlr); //p1 seat2
var de = passenger2.GetComponent(myControllerP2); //p2 seat2
seat2used = false;
passenger2.transform.position = seat2exit.position;
passenger2.transform.rotation = seat2exit.rotation;
passenger2.rigidbody.isKinematic = false;
passenger2.parent = null;
if(de) //player2
{
de.nut2(myControllerP2);
cameraB.enabled = false;
personCamB.enabled = true;
}
else //player1
{
da.nut2(physContrlr);
cameraA.enabled = false;
personCamA.enabled = true;
}
}
}
function deadpassenger()
{
seat2used = false;
}
function deaddriver()
{
seat1used = false;
}
When posting code please highlight the code and press the "10101" button. It will make the code look nice and readable. I will do this for you.