Hi I'm making a first person 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 too.So overall I need a script to let a character get in ,control, then get out of a car.
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!
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.
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;
}
OBS: MovScript is my variable of movment script of my player