Magnet script for UNITY 3D

回眸只為那壹抹淺笑 提交于 2019-12-04 17:24:59

Assuming you are able to read the magnet input correctly. This is how I did an FPS style controller script:

  1. In Unity5 import the asset package Standard Assets/Characters.
  2. Create an instance of RigidBodyFPSController.prefab from that package.
  3. Remove it's child object, "MainCamera"
  4. Import the Google cardboard unitypackage.
  5. Replace the "MainCamera" you removed in step #3 with CardboardMain.prefab
  6. Update or modify a copy of RigidbodyFirstPersonController.cs GetInput() method.

GetInput() with Google Cardboard forward movement fallback:

private Vector2 GetInput()
{
    Vector2 input = new Vector2
    {
        x = Input.GetAxis("Horizontal"),
        y = Input.GetAxis("Vertical")
    };

    // If GetAxis are empty, try alternate input methods.
    if (Math.Abs(input.x) + Math.Abs(input.y) < 2 * float.Epsilon)
    {
        if (IsMoving) //IsMoving is the flag for forward movement. This is the bool that would be toggled by a click of the Google cardboard magnet
        {
            input = new Vector2(0, 1); // go straight forward by setting positive Vertical
        }
    }
    movementSettings.UpdateDesiredTargetSpeed(input);
    return input;
}

Google's SDK only support's detecting a magnet "click". If you want to hold down the magnet to move forward, I recommend using Cardboard Controls+ from the Unity3D Asset Store.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!