【问题标题】:Magnet script for UNITY 3DUNITY 3D 的磁铁脚本
【发布时间】:2015-01-11 18:43:38
【问题描述】:

我正在开发一个 Google Cardboard 项目,现在我有一个 Android 演示,您可以在其中查看我在 UNITY 3D 中构建的特殊场景,一切正常且看起来不错,但我真正想要的是:

当我按下 Google Cardboard 磁铁按钮时,我想向前走。

我在网上找到了一些脚本,但我不知道如何让这些脚本在我的 UNITY 项目中运行。

谁能帮助我进一步解决这个问题?

【问题讨论】:

  • GitHub 上的用户 CaseyB 为此发布了一个实用程序,它使用 Unity 的事件系统-github.com/CaseyB/UnityCardboardTrigger/tree/develop 在对象上使用此脚本,事件 OnCardboardTrigger() 将在附加到同一对象的其他脚本上运行,当按钮被按下
  • 嘿阿古曼德,谢谢你的回复! :) 如果我把脚本放在我的 FirstPersonController 上,我应该怎么做才能让控制器移动?
  • 在你的脚本中加入一个名为OnCardboardTrigger()的函数,然后在该函数中放置你的运动控制。据我所知,这个函数会在你按下按钮的每一帧触发,这样移动控制就可以像transform.position += transform.forward * Time.deltaTime * speed;
  • 嘿阿古曼德!我试着按照你说的去做,但我认为它仍然不起作用。我的手机没有任何迹象表明看到磁铁..即使我尝试 Application.quit();我将来自 GitHub 的脚本放在我的 firstpersoncontroller 上,并添加了一个名为:Button_press 的脚本。也许我正在做一些非常错误的事情,我希望你能告诉我,gehehe.. [link] (nicoabbink.nl/screens/1.png) [/link] link [/link]
  • 嗨..我已经在 google cardboard vr 表面设置了一个图像.. 并且放大和缩小过程也.. 我正在研究头部运动但没有成功.. 如果你有任何想法然后帮助我。

标签: android unity3d magnetometer virtual-reality google-cardboard


【解决方案1】:

假设您能够正确读取磁铁输入。这就是我制作 FPS 风格控制器脚本的方式:

  1. 在 Unity5 中导入资产包 Standard Assets/Characters。
  2. 从该包创建 RigidBodyFPSController.prefab 的实例。
  3. 删除它的子对象“MainCamera”
  4. 导入Google cardboard unitypackage
  5. 将您在第 3 步中移除的“MainCamera”替换为 CardboardMain.prefab
  6. 更新或修改 RigidbodyFirstPersonController.cs GetInput() 方法的副本。

GetInput() 与 Google Cardboard 向前移动后备:

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 的 SDK 仅支持检测磁铁“点击”。如果你想按住磁铁向前移动,我推荐使用 Unity3D Asset Store 中的Cardboard Controls+

【讨论】:

  • 我没有 IsMoving 标志。说,当前上下文中不存在
  • 在上面引用IsMoving的代码行上有一个代码注释。此注释用于解释该行代码。在这种情况下,什么会设置 IsMoving 的值。您在 IsMoving 中的范围取决于您,您可以在上面的代码块顶部添加 public bool IsMoving;
猜你喜欢
  • 1970-01-01
  • 2020-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多