- Home /
getting out of a vehicle
I have a turret that I want to mount on the side of a ship. When i go up to a computer I want to be able to hit e and the player can take control of it. I have three seperate scripts. the first on goes onto the computer.
var showGUI : boolean; var other : Transform; var controlingturret=false;
function OnMouseEnter() { showGUI = true;
}
function OnMouseExit() { showGUI = false; }
function OnGUI() { if (showGUI) { GUI.Box(Rect(20, 10, 150, 100), "Press E to Enter"); if (Input.GetButtonDown("Use")){ GameObject.FindWithTag("turret1").GetComponent(TurretControl).enabled=true; GameObject.FindWithTag("Player").GetComponent(which).DoSomething(); controlingturret=true; }
}
}
if (controlingturret){ if (Input.GetButtonDown("Use")){ GameObject.FindWithTag("turret1").GetComponent(TurretControl).enabled=false; GameObject.FindWithTag("Player").GetComponent(which).DoSomethingAgain(); controlingturret=false; } }
the second goes onto the player to control the camera selection
var cam1 : Camera; var cam2 : Camera;
function Start () {
cam1.enabled=true;
cam2.enabled = false;
}
function DoSomething() {
cam1.enabled=false;
cam2.enabled = true;
}
function DoSomethingAgain() {
cam1.enabled=true;
cam2.enabled=false;
}
and the third goes onto the turret itself
var horizontalSpeed = 2.0; var verticalSpeed = 2.0; function Start (){ GetComponent(TurretControl).enabled=false; }
function Update () { // Get the mouse delta. This is not in the range -1...1
var h = horizontalSpeed * Input.GetAxis ("Mouse X");
var v = verticalSpeed * Input.GetAxis ("Mouse Y");
transform.Translate (h, 0, v);
}
So far I am able to get into the turret and control it just fine but i'm not able to take control of the player again. Am I going about this the wrong way? Is there a more effective way to do it? Anyone got any help? Thanks
Answer by GesterX · Feb 08, 2011 at 10:31 PM
Your last bit of code is not in a function. You should create an Update function like this:
function Update()
{
if (controlingturret){
if (Input.GetButtonDown("Use")){
GameObject.FindWithTag("turret1").GetComponent(TurretControl).enabled=false;
GameObject.FindWithTag("Player").GetComponent(which).DoSomethingAgain();
controlingturret=false;
}
}
}
thats more or less what i ended up doing but i had to add a little time between when it checks for it.
Answer by EMac · Jan 24, 2011 at 07:57 PM
It looks like the last block of code in your first script is not inside a function. Remove the '}' just before it.
nope. when i do that it it seems like it just goes straight through and starts the control and then turns it off without ever letting me do anything.