在真实设备中运行时,Unity VR Player相机无法移动

时间:2017-06-23 11:54:47

标签: c# unity3d google-vr

我是Unity的初学者,并遵循Udemy教程,使用Unity学习VR并制作名为 Ninja Slash 的游戏。在 Unity Editor播放模式中,一切似乎都运行正常但是当我在真实设备中运行我的应用时,我的播放器根本无法移动即可。我无法弄清楚这里有什么问题。

这是我的播放器脚本。

public class Player : MonoBehaviour {

public GameObject sword;
public float speed = 4.5f;
public float walkingAmplitude = 0.25f;
public float walkingFrequency = 2.0f;
public float swordRange = 1.75f;
public float swordCooldown = 0.25f;

public bool isDead = false;
public bool hasCrossedFinishLine = false;

private float cooldownTimer;
private Vector3 swordTargetPosition;

// Use this for initialization
void Start () {
    swordTargetPosition = sword.transform.localPosition;
}

// Update is called once per frame
void Update () {
    if (isDead) {
        return;
    }

    transform.position += Vector3.forward * speed * Time.deltaTime;
    transform.position = new Vector3(
        transform.position.x,
        1.7f + Mathf.Cos(transform.position.z * walkingFrequency) * walkingAmplitude,
        transform.position.z
    );

    cooldownTimer -= Time.deltaTime;

    if (GvrViewer.Instance.Triggered ) {
        RaycastHit hit;

        if (cooldownTimer <= 0f && Physics.Raycast(transform.position, transform.forward, out hit)) {
            cooldownTimer = swordCooldown;

            if (hit.transform.GetComponent<Enemy> () != null && hit.transform.position.z - this.transform.position.z < swordRange) {
                Destroy (hit.transform.gameObject);
                swordTargetPosition = new Vector3 (-swordTargetPosition.x, swordTargetPosition.y, swordTargetPosition.z);
            }
        }
    }

    sword.transform.localPosition = Vector3.Lerp (sword.transform.localPosition, swordTargetPosition, Time.deltaTime * 15f);
}

void OnTriggerEnter (Collider collider) {
    if (collider.GetComponent<Enemy> () != null) {
        isDead = true;
    } else if (collider.tag == "FinishLine") {
        hasCrossedFinishLine = true;
    }
}}

以下是层次结构视图的屏幕截图。

enter image description here

我尝试使用不同版本的gvr sdk unity包,但我每次都得到相同的结果,我也试过运行不同的设备,如htc,samsung等。

1 个答案:

答案 0 :(得分:0)

我相信你无法在VR模式下改变主摄像机的变换。你必须将maincamera作为另一个对象的子(空?!),然后移动该对象。