使用带有Unity的Google Cardboard我可以在点击时设置自动跟踪但是当我环顾四周时无法改变方向

时间:2016-03-15 16:50:59

标签: unity3d unity5 google-cardboard

我正在为使用Unity的Google Cardboard开发3D环境。我已经将本教程用作我的基础:http://danielborowski.com/posts/create-a-virtual-reality-game-for-google-cardboard/

我找到了一个代码段,允许我在点击时触发自动跟踪:

void OnEnable(){
    Cardboard.SDK.OnTrigger += TriggerPulled;
}

void OnDisable(){
    Cardboard.SDK.OnTrigger -= TriggerPulled;
}
void TriggerPulled() {
    checkAutoWalk = !checkAutoWalk;
}

然而,当我环顾四周时,我一直朝原来的方向走去。意思是我开始向前走,但当我转过身时,我正在向后走。

1 个答案:

答案 0 :(得分:0)

你好我一年前做过这样的事情,不知道这是不是正确的做法。

首先,我创建了一个名为Autowalk的新脚本,将其附加到 Head 游戏对象。

using UnityEngine;

namespace Assets.Scripts
{
    public class Autowalk : MonoBehaviour
    {
        public bool Triggered;
        private FPSInputController _fpsController;

        // Use this for initialization
        void Start ()
        {
            _fpsController = GetComponent<FPSInputController>();
        }

        // Update is called once per frame
        void Update () {
            // use Triggered to test in the inspector
            if(Cardboard.SDK.CardboardTriggered){
                _fpsController.checkAutoWalk = !_fpsController.checkAutoWalk;
            }
        }
    }
}

我的FPSController脚本如下所示,这里我改变方向,取决于我面对的方向并将其应用于CharacterMotor。

using UnityEngine;

// Require a character controller to be attached to the same game object

namespace Assets.Scripts
{
    [RequireComponent(typeof (CharacterMotor))]
    [AddComponentMenu("Character/FPS Input Controller")]
    public class FpsInputController : MonoBehaviour
    {
        private CharacterMotor _motor;
        public bool CheckAutoWalk;

        // Use this for initialization
        private void Awake()
        {
            _motor = GetComponent<CharacterMotor>();
        }

        // Update is called once per frame
        private void Update()
        {
            // Get the input vector from keyboard or analog stick
            Vector3 directionVector;
            if (!CheckAutoWalk)
            {
                directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            }
            else
            {
                directionVector = new Vector3(0, 0, 1);
            }

            if (directionVector != Vector3.zero)
            {
                // Get the length of the directon vector and then normalize it
                // Dividing by the length is cheaper than normalizing when we already have the length anyway
                var directionLength = directionVector.magnitude;
                directionVector = directionVector/directionLength;

                // Make sure the length is no bigger than 1
                directionLength = Mathf.Min(1.0f, directionLength);

                // Make the input vector more sensitive towards the extremes and less sensitive in the middle
                // This makes it easier to control slow speeds when using analog sticks
                directionLength = directionLength*directionLength;

                // Multiply the normalized direction vector by the modified length
                directionVector = directionVector*directionLength;
            }

            // Apply the direction to the CharacterMotor
            _motor.inputMoveDirection = transform.rotation*directionVector;
            _motor.inputJump = Input.GetButton("Jump");
        }
    }
}

检查器的GameObject结构和视图,附带脚本:

structure inspector

如果你有任何问题可以随意提问;)

干杯 托比