首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >虚拟现实添加运动:步行

虚拟现实添加运动:步行
EN

Stack Overflow用户
提问于 2018-03-07 10:19:57
回答 1查看 87关注 0票数 3

使用Unity2017.3.1f1个人(64位)为Android构建VR应用程序,使用Cardboard VR SDK。该应用程序的目的是让用户以身临其境的方式可视化数据。

目前,想做一些类似的场景视图,一个人可以前进,走到一个人看到的地方,但与卡板单一按钮,我只是提供能力向前推进。

在画布中,用户可以选择他/她想要的移动方式:传送或步行。

如你所见,传送器工作得很好。

当用户选择walk时,控制台中会显示以下错误:

NullReferenceException:未设置为对象实例的对象引用 Player.TryWalk () (atAsset/Player.cs:44) Player.Update () (at Asset/Player.cs:37)

我的玩家剧本:

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum InputMode
{
    NONE,
    TELEPORT,
    WALK
}

public class Player : MonoBehaviour {

    public static Player instance; //Singleton design pattern: only one instance of the class appears
    public InputMode activeMode = InputMode.NONE;

    [SerializeField]
    private float playerSpeed = 3.0f;

    void Awake()
    {
        if(instance != null)
        {
            GameObject.Destroy(instance.gameObject);
        }
        instance = this;
    }

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update ()
    {
        TryWalk();
    }

    public void TryWalk()
    {
        if(Input.GetMouseButton(0) && activeMode == InputMode.WALK)
        {
            Vector3 forward = Camera.main.transform.forward;

            Vector3 newPosition = transform.position + forward * Time.deltaTime * playerSpeed;

            transform.position = newPosition;
        }
    }
}

作为Player的组件添加了Player脚本:

当按下“行走”按钮时,活动模式将更改为“步行”,如您在下一个图像中所看到的那样。

不过,即使发生了这种情况,用户也无法行走。

,我能做些什么来解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-07 10:30:56

Camera.main;返回null。

为了修复它,必须让相机在我的场景中标记为MainCamera,就像你在下一张图片中看到的那样。

看它在这里起作用。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49149424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档