How to jump on to a turret gun

Okay so this is a MASSIVE question and would help me a number amount in my game :)… To go in to detail, i have modelled a turret gun, and When i approach the This turret gun i want it so what basicly when i hit a box collider that acts as a trigger or somthing, i can press like “X” to jump on to the turret gun Then hit “X” again to jump off of the turret gun, it anybody could help me then

Thanks

I suppose this is the classic “first person enters the vehicle” case: you move around as a first person character, but when approaching the vehicle you may “enter” and drive it - or control the turret, in your case.

You should child an empty object to the turret - let’s call it “PlayerPos” - to indicate the position and rotation of the player when on the turret. When you press the “jump to turret” key, a special function disable the player movement script, move/rotate the player to “PlayerPos”, child it to “PlayerPos” and enable the turret control script.

To jump off the turret, you must have another empty object childed to the turret to define the “out of turret” position - let’s call it “PlayerExit”. Another function disable the turret control script, move/rotate the player to “PlayerExit”, unchild it and enable the player movement script.

To ensure this will happen only when the player is next to the turret, a trigger is used: when the player enters the trigger, the variable player is set to the player transform and canJump enables the “jump on turret” key.

Child a rectangular trigger to the turret, and attach this script to the trigger object:

private var playerPos: Transform;
private var playerExit: Transform;
private var player: Transform;
private var onTurret = false;
private var canJump = false;
private var jumping = false; // signals when the player is jumping

function JumpOn(duration: float){ // jump on the turret
  jumping = true; // signals that the player is jumping
  // disable MovePlayerScript (replace with actual name)
  player.GetComponent.().enabled = false;
  var startPos = player.position;
  var startRot = player.rotation;
  var t: float = 0; 
  while (t < 1){ // move the player to playerPos
    t += Time.deltaTime/duration;
    player.position = Vector3.Lerp(startPos, playerPos.position, t);  
    player.rotation = Quaternion.Slerp(startRot, playerPos.rotation, t);  
    yield;
  }
  player.parent = playerPos; // child player to playerPos
  onTurret = true; // tell it's on the turret
  // enable TurretControl script (replace with the actual name)
  transform.parent.GetComponent.().enabled = true;
  jumping = false; // jump finished
}

function JumpOff(duration: float){
  jumping = true; // warns that the player is jumping
  // disable TurretControl script (replace with the actual name)
  transform.parent.GetComponent.().enabled = false;
  var center = player.position - Vector3.up; // set the Slerp center
  var startPos = player.position - center; // start and end pos are
  var endPos = playerExit.position - center; // relative to center
  var startRot = player.rotation;
  var t: float = 0;
  while (t < 1){
    t += Time.deltaTime/duration;
    // add center to make the point relative to the world
    player.position = Vector3.Slerp(startPos, endPos, t) + center;  
    player.rotation = Quaternion.Slerp(startRot, playerExit.rotation, t);  
    yield;
  }
  player.parent = null; // unchild the player
  onTurret = false; // player is no more on the turret
  // enable MovePlayerScript (replace with the actual name)
  player.GetComponent.().enabled = true;
  jumping = false; // player ended jumping
  player = null; // disable jump
}

// on trigger enter identifies the player and enables jump on
function OnTriggerEnter(col: Collider){ 
  if (col.tag == "Player"){
    player = col.transform; // set the player variable and enable jump
    canJump = true;
  }
}

function OnTriggerExit(col: Collider){ 
  if (col.tag == "Player"){
    canJump = false;  // no jump if outside trigger
  }
}

function Start(){
  transform.parent.GetComponent.().enabled = false;
  playerPos = transform.parent.Find("PlayerPos");
  playerExit = transform.parent.Find("PlayerExit");
}

function Update(){
  if (!jumping){ // do nothing if already jumping
    if (onTurret){ // if on turret, jumps off
      if (Input.GetKeyDown("x")){
        JumpOff(0.6);
      }
    } else { // if off turret, jumps on
      if (canJump && Input.GetKeyDown("x")){
        JumpOn(0.8);
      }
    }
  }
}

In a brief, you must child to the turret: a box trigger, an empty object called “PlayerPos” and an empty object called “PlayerExit”. Attach the script above to the trigger object, and adjust “PlayerPos” to the position and direction the player must be when in the turret, then adjust “PlayerExit” to the position and orientation the player will “land” when jumping off. Finally, name the player movement and turret control scripts as “MovePlayerScript.js” and “TurretControl.js”.

Semi Pseudo Scripting Answer:

A. If you are thinking of a way to judge distance by using the collider i would recommend creeating a script that checks the difference of the Turret and the collider.
so:

`
var rangeOfGettingInTurret : float;
var player : GameObject;

private var distance : float;

function update()
{
distance = Vector3.Distance(transform.position,player.transform.position);

if(distance <= rangeOfGettingInTurret)
{
/*Do What needs to be done about checking input and such*/
}

}
`

Just to clarify that was something i just thought of and probably wont work like that…

B.Here are some things you should check out.

Input.GetButton()

Vector3.Distance()

I hope i could help or spark something! :smiley: