Player get in/out of vehicle

Hi there,
I am working on a FPS type of game, and I want the player character to be able to get in/out of drivable vehicles. Can anyone help me perhaps?

I know it has been done before, but looks like the thread is not active anymore
http://forum.unity3d.com/threads/153157-Player-enter-exit-vehicle

Best regards,
Jakes

Well looking at that video, i think all that happens is a trigger on the doors of the car, activating that trigger simply switches of the fps player and camera and enables car controls and car camera.

I have limited knowledge to scripting, but will try. Thanks

Ok, I have the following:

  • Player called Jimmy (Tagged as Player)
  • a Tank, called Ratel90, also tagged as Player

Both are controllable. When I run my project, the tank and character react on the same inputs.

I have the following javascript, but somehow it does not work.

// This script is for getting in and out of vehicles//

var player : Transform ; // drag the player here
var enterRange: float = 8.0; // distance to enter the tank
var exitPos = Vector3(0, 1.5, 0); // exit position relative to the tank

private var cam : Camera; // tank camera
private var inTank = false; // tells when the character is inside the tank

function Start(){
  cam = GetComponentInChildren(Camera);
  cam.enabled = false; // make sure the tank camera is off
}

function EnterTank(){ // enters the tank:
  player.gameObject.SetActiveRecursively(false); // deactivate the original player...
  cam.enabled = true; // and activate the tank camera...
  inTank = true; // and controls
}

function ExitTank(){
  // move the player to its exiting position:
  player.position = transform.TransformPoint(exitPos);
  player.gameObject.SetActiveRecursively(true); // reactivate the original player...
  cam.enabled = false; // and deactivate the tank camera...
  inTank = false; // and the tank controls
}

function Update(){
  if (inTank == false); // if outside the tank...
    // and E pressed while player inside the enter range:
    if (Input.GetKeyDown("e")  Vector3.Distance(player.position, transform.position));
    
}

What am I doing wrong?

I would say in you Start () to disable your tank script

1 Like