- Home /
Toggle Control between multiple turrets
I have a ship that has multiple turrets on it, each turret its own first person camera. I want the player to only control one at a time however, if he is not in control then it can either be left unoccupied or controlled by an AI. I'm having an issue toggling between them right now. There are so far two scripts. The first controls the ship - movement, player occupation, turret states, the second script controls the turret itself - shooting, rotation, ai and such. So far I have three turret prefabs that I want to rotate between by pressing a keyboard button. Here's what I have so far.
playerController
void FixedUpdate (){
if (Input.GetKeyDown("t")){
NextCamera();
}
}
void RefreshCameras() {
int i;
for(i=0;i<playerWeapons.Length;i++) {
if(i==camIdx) {
// playerWeapons[i] ACTIVE, SET CONTROLLER TYPE TO MOUSE AND TURN ON CAMERA.
} else {
// playerWeapons[i] INACTIVE, DISABLE ALL CAMERAS AND SET CONTROLLER TYPE TO AI.
}
}
}
void NextCamera() {
if(camIdx<playerWeapons.Length-1) camIdx++;
else camIdx = 0;
RefreshCameras();
}
turretController
The turret controller determines its state using this.
public enum controllerTypes {Mouse = 1, AI = 2, Empty = 3 }
public static controllerTypes controllerType = controllerTypes.Mouse;
What I'm trying to accomplish is have the controllerType update itself so that when the player is using turret 2, it will become controllable with the mouse instead of AI / Empty. I have the code to get the turrets working, just not the code to switch between them.
Here's the controller for the mouse for extra insight.
void MouseUpdate (){
if (!(controllerType == controllerTypes.Mouse)) return;
if (mouseAxes == RotationAxes.MouseXAndY)
{
rotationX += Input.GetAxis("Mouse X") * mouseSensitivityX;
rotationX = Mathf.Clamp (rotationX, maxLookLeft, maxLookRight);
rotationY += Input.GetAxis("Mouse Y") * mouseSensitivityY;
rotationY = Mathf.Clamp (rotationY, maxLookDown, maxLookUp);
turretBody.localEulerAngles = new Vector3(0, rotationX, 0);
turret.localEulerAngles = new Vector3(-rotationY, 0, 0);
}
else if (mouseAxes == RotationAxes.MouseX)
{
rotationX += Input.GetAxis("Mouse X") * mouseSensitivityX;
rotationX = Mathf.Clamp (rotationX, maxLookLeft, maxLookRight);
turretBody.localEulerAngles = new Vector3(0, rotationX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * mouseSensitivityY;
rotationY = Mathf.Clamp(rotationY,maxLookDown,maxLookUp);
turret.localEulerAngles = new Vector3(-rotationY, 0, 0);
}
}