Okay, so I’m making this FPS game but you don’t start out with any guns. Instead you walk up to a turret and shoot all the zombies. But I have no idea what to say in the script for the character to walk up to the turret and ‘enter’ it! I would like the player to push the space bar to enter it, and to exit it. PLEASE HELP!!! thank you. I’m a total noob when it comes to scripting.
See:
function Update () {
if (Input.GetButton (“Space”))
enter browning
}
Anyhoo, this right here is a very ineffecient bit of code, but it should help give you at least one idea of how to go about this.
Player.js
static var instance : Player = null;
var maxDistToEnterTurret : float;
function Start() {
if ( instance != null ) throw ("Two active Player objects!")
instance = this;
}
function Update () {
if ( Input.GetKeyDown( KeyCode.Space ) ) {
var allTurrets : Array = GameObject.FindObjectsOfType( Turret );
var closest : Turret;
var closeness : float = maxDistToEnterTurret;
for ( var t : Turret in allTurrets ) {
var d : float = Vector3.Distance( transform.position, t.transform.position );
if ( d <= closeness ) {
closest = t;
closeness = d;
}
}
if ( closest ) {
this.enabled = false;
closest.Control();
}
}
// other player controls go here
}
Turret.js
function Control() {
var control : boolean = true;
while ( control ) {
if ( Input.GetKeyDown( KeyCode.Space ) ) {
Player.instance.enabled = true;
control = false;
}
// other turret controls go here
yield;
}
}
THANK YOU!
But I still have problems. And am I supposed to be able to walk up to the turret and push the “space” button to enter?
Then again thank you for your help.