Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 使用谷歌纸板和Unity,我可以在点击时设置自动行走,但可以';当我环顾四周时,不要改变方向_Unity3d_Unity5_Google Cardboard - Fatal编程技术网

Unity3d 使用谷歌纸板和Unity,我可以在点击时设置自动行走,但可以';当我环顾四周时,不要改变方向

Unity3d 使用谷歌纸板和Unity,我可以在点击时设置自动行走,但可以';当我环顾四周时,不要改变方向,unity3d,unity5,google-cardboard,Unity3d,Unity5,Google Cardboard,我正在与Unity一起为谷歌纸板制作一个3D环境。我将本教程用作我的基础: 我发现了一段代码片段,允许我在单击时触发自动漫游: void OnEnable(){ Cardboard.SDK.OnTrigger += TriggerPulled; } void OnDisable(){ Cardboard.SDK.OnTrigger -= TriggerPulled; } void TriggerPulled() { checkAutoWalk = !checkAutoW

我正在与Unity一起为谷歌纸板制作一个3D环境。我将本教程用作我的基础:

我发现了一段代码片段,允许我在单击时触发自动漫游:

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

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

然而,当我环顾四周时,我总是沿着原来的方向走。这意味着我开始向前走,但当我转身时,我就向后走了。

嗨,我一年前做过类似的事情,不知道这是否仍然是正确的方式

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

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;
            }
        }
    }
}
使用UnityEngine;
命名空间资产.脚本
{
公共级自动人行道:单行为
{
公共布尔触发;
专用FPS输入控制器\FPSU控制器;
//用于初始化
无效开始()
{
_fpsController=GetComponent();
}
//每帧调用一次更新
无效更新(){
//使用触发器在inspector中进行测试
if(carboard.SDK.Cardboard已触发){
_fpsController.checkAutoWalk=!\u 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");
        }
    }
}
使用UnityEngine;
//要求将角色控制器连接到同一游戏对象
命名空间资产.脚本
{
[要求组件(类型(CharacterMotor))]
[AddComponentMenu(“字符/FPS输入控制器”)]
公共类FPInputController:单行为
{
私人汽车;
公共厕所自动人行道;
//用于初始化
私人空间
{
_电机=GetComponent();
}
//每帧调用一次更新
私有void更新()
{
//从键盘或模拟棒获取输入向量
向量3方向向量;
如果(!检查自动行走)
{
directionVector=newvector3(Input.GetAxis(“水平”),0,Input.GetAxis(“垂直”);
}
其他的
{
方向向量=新向量3(0,0,1);
}
if(directionVector!=Vector3.zero)
{
//获取directon向量的长度,然后对其进行规格化
//当我们已经有了长度时,除以长度比标准化要便宜
var directionLength=directionVector.magnity;
方向向量=方向向量/方向长度;
//确保长度不大于1
方向长度=数学最小值(1.0f,方向长度);
/使输入向量对极端敏感,而在中间不敏感。
//这使得使用模拟棒时更容易控制低速
方向长度=方向长度*方向长度;
//将归一化方向向量乘以修改后的长度
方向向量=方向向量*方向长度;
}
//将方向应用于CharacterMotor
_motor.inputMoveDirection=变换.旋转*方向向量;
_motor.inputJump=Input.GetButton(“跳转”);
}
}
}
游戏对象结构和检查器视图,以及附加的脚本:

如果您还有其他问题,请随时提问;)

干杯 托比