Используя Google Cardboard с Unity, я могу настроить автоматическое перемещение по клику, но не могу изменить направление, когда оглядываюсь

Я работаю над 3D-средой для Google Cardboard с Unity. Я использовал это руководство в качестве основы: 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;
}

Однако, когда я оглядываюсь, я продолжаю идти в первоначальном направлении. Это означает, что я начинаю идти вперед, но когда я поворачиваюсь, я иду назад.


person dcp3450    schedule 15.03.2016    source источник


Ответы (1)


Привет, я сделал что-то подобное год назад, не знаю, правильный ли это способ сделать это.

Сначала я создал новый скрипт под названием 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 и вид инспектора с прикрепленными скриптами:

структура инспектор

Если у вас есть еще вопросы, не стесняйтесь спрашивать ;)

Привет Тоби

person the_tobster    schedule 03.06.2016