Reading Controller Buttons
To control various between game actions (e.g. restart game, exit game) we’ll read the SymGym controller buttons and take the appropriate actions.
We’ll get button states in the Update() method of the GameController.cs script.
void Update() {
if (sgStub != null) {
sgState = sgStub.Call<AndroidJavaObject>("getState");
bool leftStickLeftButton = sgState.Call<bool>("isLeftLeft");
bool rightStickTrigger = sgState.Call<bool>("isRightTrigger");
bool leftStickTrigger = sgState.Call<bool>("isLeftTrigger");
// restart game at any time if left button on the left control stick pressed
if (leftStickLeftButton) {
Debug.Log("Forcing scene: " + SceneManager.GetActiveScene().buildIndex);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
if (gameOver) {
restartText.text = "Press Trigger to Restart";
restart = true;
}
// restart game if game over and either trigger pressed
if (restart) {
if (rightStickTrigger|| leftStickTrigger) {
Debug.Log("Loading scene: " + SceneManager.GetActiveScene().buildIndex);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
int lLegVel = sgState.Call<int>("getLeftLegVelocity");
int rLegVel = sgState.Call<int>("getRightLegVelocity");
if ((lLegVel > velToFire || rLegVel > velToFire) && Time.time > nextFire) {
nextFire = Time.time + fireRate;
rc.Fire();
Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
GetComponent<AudioSource>().Play();
}
}
}
- Previous
- Next