Reading Controller Position

The SymGym API allows you to read the position and velocity of each limb as discussed in the SymGym Game API section.

In order to read the limb positions/velocities, first get an sgState object from the sgStup. This is typically done in the Upate() method. For our SpaceShooter example:

We want to use the SymGym controller to control the player, so let’s add the following code to the PlayerController.cs scripte. (If you are following the tutorials, you may need to modify the Done_PlayerController.cs script in the Done_Scripts directory.)

First, we need to add the SymGym variables to the script. And a value that sets the minimum velocity needed to fire a shot. We’ll make this public so we adjust it from the Unity UI.

  private static AndroidJavaObject sgStub;
  private static AndroidJavaObject sgState;

  public int velToFire = 30;    // minimum pedal velocity to fire shot
  public float horizontalSpeed  // ship left/right movement speed adjustment

The library should only be initialized once; we initialized it in the GameController.cs Initialize() method. We’ll retrieve that instance in the Start() method

  void Start() {
  sgStub = Done_GameController.sgStub;
  if (sgStub == null) {
    Debug.Log ("Cannot find SG stub");

We want to know whether to move the player left or right. For that we need the arm lever positions. We’ll get them in the FixedUpdate() method of the PlayerController.cs script.

  void FixedUpdate (){
    // make sure we have a SymGym stub instance.
    if (sgStub != null) {

      // get the current state of the SymGym controller and the arm lever positions
      sgState = sgStub.Call<AndroidJavaObject>("getState");
      int lArmPos = sgState.Call<int>("getLeftArmPosition");
      int rArmPos = sgState.Call<int>("getRightArmPosition");

      // use the difference between the positions of the arm levers to determine ship left/right movement
      // (the farther apart the levers are the faster left/right the ship moves.
      float vHorizontal = (float)((rArmPos - lArmPos) / horizontalSpeed);

      // no vertical movement for now
      float vVertical = -1.0f;

      // use the velocities from the arm levers to calculate new ship position (from original SpaceShooter)
      Vector3 movement = new Vector3(vHorizontal, 0.0f, vVertical);
      rb.velocity = movement * speed;
      rb.position = new Vector3(Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
                                Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax));
      rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);