x


FPS getting in,out and controlling a vehicle.

Hi I'm making a first person http://shooter.So far characters can walk around, shoot ai's and pick up stuff but I'd like to let them get in vehicles.I don't have a script for a car or anything so I'd need that http://too.So overall I need a script to let a character get in ,control, then get out of a car.

more ▼

asked Apr 25 '10 at 09:30 PM

Joe 3 gravatar image

Joe 3
33 3 3 8

Don't ask for much do you?

Apr 25 '10 at 11:47 PM spinaljack
(comments are locked)
10|3000 characters needed characters left

6 answers: sort voted first

Ok, this is just theoric, but it could be something as simple as this.

Create a singleton with a property or a global variable with the name "Controller" where controller is a gameobject

Create a car object (something very simple would do) that can be driven by the player if that gameobject is the same than Controller

if (this==Controller) then ...

Create a character object that can also be controlled by the player if the gameobjeect is the same as controller

Create a camera script that points to "Controller" all the time.

Presto! now all you have to do is a global object or a coroutine that checks a key to switch the controller variable from one gameobject to the another. Neat!

Ok now that you've done that make sure that the key switches from player to car only if they are at a certain distance. (you could even check if you are near the door)

Now for the final trick

Ok one way to do this easily is to simply dissapear the character y not rendering it whenever he is not the controller but lets try something more elegant.

Create a new transform child object in the car and place it where the seat of the player would be lets name this "seat" (it could also be a seat model)

Change the script so it makes the seat transform into the parent object of our character when the car is the controller and place the character at position 0,0,0 (if your character is animated you could make it sit)

character.transform.parent=vehicle.seat; //where seat is the seat transform

Theres a problem though, when you "exit the car" the character would still be in the same position 0,0,0, which means it would still be inside the car, to fix this move it to your left or right vector by a few units (if the character is still sitting make it stand). That way your character will exit the car whenever he is out. (this could also be used in case you decided to just dissapear the character so it moves around with the vehicle)

Thats it, is a pretty simple but functional way to move characters around in vehicles. Try it!

more ▼

answered May 05 '10 at 11:11 PM

Azrael gravatar image

Azrael
218 5 7 9

(comments are locked)
10|3000 characters needed characters left

There's actually a lot of info on this out there already. This tutorial in particular is excellent.

more ▼

answered Apr 25 '10 at 11:02 PM

dhendrix gravatar image

dhendrix
2.2k 25 34 59

but how would i get a character to get in and out out of it?

Apr 26 '10 at 02:33 AM Joe 3
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Oct 24 '10 at 12:25 AM

oma gravatar image

oma
11 1

(comments are locked)
10|3000 characters needed characters left

to make the character get inside I suggest you use the position of your seat object and move your character there, probably localPosition will be better for this. Once there, change the animation to a "sitting animation" on loop, crossfade it with the character moving his arms a bit(don't loop this one) when you press left and right for a better driving experience. To exit, you can either a: create 3 Random.Ranges with let's say 1,3 and subtract it from the player transform.position to make it exit a few inches from the driver seat, then change the animation back to standing.

more ▼

answered Oct 24 '10 at 12:48 AM

Bravini gravatar image

Bravini
1.1k 6 11 29

(comments are locked)
10|3000 characters needed characters left

Here is the script, read the script to know what do with the variables:

var MovScript : Movment;
var Player : Transform;
var Speed = 50.0;
var CarSpeed = 50.0;
var StopSpeed = 0.0;
var InCarPosition : Transform;
var PlayerIn = false;
var ExitPoint : Transform;
var CanChange = true;
var turnSpeed: float = 90;

function Update () {

transform.Rotate(0, Input.GetAxis("Horizontal")*turnSpeed*Time.deltaTime, 0);

Player = GameObject.FindWithTag("Player").transform;

if(PlayerIn == true){
Player.transform.position.y  = InCarPosition.transform.position.y;
Player.transform.position.z  = InCarPosition.transform.position.z;
Player.transform.position.x  = InCarPosition.transform.position.x;
}else{

}

if(Input.GetKey(KeyCode.W)&& PlayerIn == true){
rigidbody.velocity = transform.forward * Speed *Input.GetAxis("car");
rigidbody.velocity.y = Input.GetAxis("root");
}

if(Vector3.Distance(Player.position,transform.position) < 5){
if(Input.GetKey(KeyCode.E)&& PlayerIn == false){
if(CanChange == true){
CanChange = false;
Change();
MovScript.gravity = 0;
MovScript.runSpeed = 0;
MovScript.normalSpeed = 0;
Player.transform.position.y  = InCarPosition.transform.position.y;
Player.transform.position.x  = InCarPosition.transform.position.x;
Player.transform.position.z  = InCarPosition.transform.position.z;
PlayerIn = true;
Speed = CarSpeed;
}
}else{
if(Input.GetKey(KeyCode.E)&& PlayerIn == true){
if(CanChange == true){
CanChange = false;
Change();
MovScript.gravity = 20;
MovScript.runSpeed = 30;
MovScript.normalSpeed = 15;
PlayerIn = false;
Speed = StopSpeed;
}
}
}
}
}

function Change(){
yield WaitForSeconds(1);
CanChange = true;
}
more ▼

answered Nov 21 '12 at 07:47 PM

Matheusfig gravatar image

Matheusfig
0 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x520
x417

asked: Apr 25 '10 at 09:30 PM

Seen: 3656 times

Last Updated: Nov 21 '12 at 07:47 PM