Controller Camera Axis?

Hey guys,

I am trying to learn how to properly code a xbox controller to work. I have been using the unity/hololens controller example to learn from but one I am still having one issue with having the Analog Sticks for movement and looking that use Local position over world position. It took me a minute to figure out how to change the LeftThumbstickY() to affect the Z axis over the Y Axis. But it seems to be world coordinates.I was able to get the desired move effects using the D-Pad buttons, but I am unsure how to get it using the Analogs sticks with the Thumbstick Inputs. Any advice?

        float moveHorizontal = MoveHorizontalSpeed * controllerInput.GetAxisLeftThumbstickX();
        float moveVertical = MoveVerticalSpeed *  controllerInput.GetAxisLeftThumbstickY();
        this.transform.Translate(moveHorizontal, 0.0f, moveVertical);



        float rotateAroundY = RotateAroundYSpeed * controllerInput.GetAxisRightThumbstickX();
        float rotateAroundX = RotateAroundXSpeed * controllerInput.GetAxisRightThumbstickY();
        float rotateAroundZ = RotateAroundZSpeed * (controllerInput.GetAxisRightTrigger() - controllerInput.GetAxisLeftTrigger());
        this.transform.Rotate(-rotateAroundX, rotateAroundY, rotateAroundZ);

        if (controllerInput.GetButton(ControllerButton.DPadUp))
            this.transform.localPosition += Main.transform.forward;

        if (controllerInput.GetButton(ControllerButton.DPadDown))
            this.transform.localPosition -= Main.transform.forward;
        if (controllerInput.GetButton(ControllerButton.DPadLeft))
            this.transform.localPosition -= Main.transform.right;
        if (controllerInput.GetButton(ControllerButton.DPadRight))
            this.transform.localPosition += Main.transform.right;

Instead of using the three-argument version of transform.Translate(), use the one that takes a Vector3.

Steps:

First assign those moveHorizontal, 0.0f, moveVertical arguments to a new Vector3, say myTempVector3.

Rotate that Vector3 by your rotation, like so:

 // point my flat X/Z inputs in the direction I'm facing:
 myTempVector3 = this.transform.rotation * myTempVector3;

Finally use that in your transform.Translate() call.

1 Like