1

Hi we (myself and my students) are using MRTK in Unity to build simple VR games.

We are trying to get the xBox Controller to move the player (or in MRTK terms I think to move the scene around the camera which is fixed at 0,0,0).

I've set up the controller and played with the MRTK settings but no luck.

My controller works perfectly in Windows Mixed Reality Portal but goes dead when the game loads.

Any help on the exact steps/settings in the MRTK editor window is appreciated.

Ben

3
  • If there is an error message, could you post it so that we can locate the problem? Aug 13, 2019 at 6:12
  • Hi, could you please give a little bit more detail about your repro steps? When you say "controller" do you mean your xbox controller? What code are you using to listen for events? What do you expect to happen, what is actually happening? Aug 13, 2019 at 20:58
  • Update: Using the default controller porfile and a new official wireless Xbox controller. If HP WMR headset is plugged in you you play the game controller does not teleport (move terrain). If the the HP WMR headset is unplugged and you play the game controller moves the camera?
    – Ben Jones
    Aug 13, 2019 at 21:33

1 Answer 1

2

There are two things to solve here:

  1. How to move the player. Answer: Use MixedRealityPlayspace.Transform.Translate
  2. How to respond to gamepad input. Answer: you can use Unity's Input.GetAxis to figure out which buttons / joysticks are pressed, however I found it a bit easier to use the controller mappings in MRTK to map the "navigation action" to the gamepad dpad and joysticks, and then listen for input changed on navigation events.

You can use the following code to move the MR Playspace using gamepad:

using Microsoft.MixedReality.Toolkit;
using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.Utilities;
using UnityEngine;

/// <summary>
/// Moves the player around the world using the gamepad, or any other input action that supports 2D axis.
/// 
/// We extend InputSystemGlobalHandlerListener because we always want to listen for the gamepad joystick position
/// We implement InputHandler<Vector2> interface in order to receive the 2D navigation action events.
/// </summary>
public class MRPlayspaceMover : InputSystemGlobalHandlerListener, IMixedRealityInputHandler<Vector2>
{
    public MixedRealityInputAction navigationAction;
    public float multiplier = 5f;

    private Vector3 delta = Vector3.zero;
    public void OnInputChanged(InputEventData<Vector2> eventData)
    {
        float horiz = eventData.InputData.x;
        float vert = eventData.InputData.y;
        if (eventData.MixedRealityInputAction == navigationAction)
        {
            delta = CameraCache.Main.transform.TransformDirection(new Vector3(horiz, 0, vert) * multiplier);
        }
    }

    public void Update()
    {
        if (delta.sqrMagnitude > 0.01f)
        {
            MixedRealityPlayspace.Transform.Translate(delta);
        }
    }

    protected override void RegisterHandlers()
    {
        CoreServices.InputSystem.RegisterHandler<MRPlayspaceMover>(this);
    }

    protected override void UnregisterHandlers()
    {
        CoreServices.InputSystem.UnregisterHandler<MRPlayspaceMover>(this);
    }
}

I used the following controller mapping to have dpad and thumbstick hook up to the navigation action: enter image description here

Then I created a new gameobject, attached the MRPlayspaceMover script, and assigned the "navigation action" field:

enter image description here

3
  • Thanks, it worked a treat, my only issue now is the camera passes through a vertical face of terrain collider? I have logged an issue in the MRTK GitHub but was wondering if you know a solution (other than putting up walls of box colliders)?
    – Ben Jones
    Aug 15, 2019 at 6:48
  • I would probably use colliders and then in adjust the delta to zero if the camera was near those colliders (e.g. using Physics.CheckSphere), or use some basic heurisitcs like set delta to 0 if it would make the camera move out of some predefined region. Aug 16, 2019 at 18:37
  • Hi Julia, any chance you could give me some more guidance this has broken in MRTK 2.1.0 and we (Teacher & students) can't get navigation working? With the WMR controller D Pad or Joystick. It was so easy in RC1 but broke in 2.0.0 and 2.1.0 ? We have it as an issue in GITHUB but it's in the backlog and we really need it for a project that is being launched in a week. github.com/microsoft/MixedRealityToolkit-Unity/issues/5817
    – Ben Jones
    Nov 8, 2019 at 4:05

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.