Script for getting in and out of a tank

Hi there, I am making an FPS game and I need to add tanks and helicopters. But the thing is, i don’t know how to write a script that would allow me to enter a vehicle and get out of a vehicle when pressing the button “e” (E = Enter). Can you please tell me what to do and post a script please. I have been searching on the internet for 2 hours now and everything related that i find, i don’t understand it or it isn’t what i needed.

Since effectively entering the tank is a complex job, a simpler solution is to deactivate the original character and activate a new one inside the vehicle - the vehicle itself is actually a specialized character, whose hierarchy should be something like this:

  Tank <- "empty object" with rigidbody and collider matching the tank dimensions
    TankModel <- tank model
    Main Camera <- the tank camera

The tank script must take care of the FPS character / tank driver switching, as well as the other movement and firing controls:

var player: Transform; // drag the player here
var speed: float = 5.0; // tank moving speed
var turnSpeed: float = 30; // tank turning speed in degrees/second
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) <= enterRange){
      EnterTank(); // enters the tank
  } else { // but if already inside the tank...
    // if E pressed, get out of the tank:
    if (Input.GetKeyDown("e")){
    } else { // else control the tank:
      // movement control code (example):
      transform.Rotate(0, Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime, 0);
      var vel = transform.forward * Input.GetAxis("Vertical") * speed;
      rigidbody.velocity = Vector3(vel.x, rigidbody.velocity.y, vel.z);
      // place here other control code, like turret rotation, 
      // cannon elevation, firing etc.

If you come across a problem, just think about what has to happen. In this case:

  1. check if the player is looking at a tank/helicopter and is close enough
  2. hide the tank/helicopter model
  3. put the camera on the tank’s/helicopter’s position/rotation
  4. change the control scheme from standard FPS to tank/helicopter

Putting this in code-form might not be trivial, but also not too hard, methinks.