2
\$\begingroup\$

I am following Udemy tutorials for learning VR using Unity and making a game named Ninja Slash. Everything seems to be working fine in the Unity Editor Play mode but when I run my app in a real device, my player doesn't move at all. I can't figure out what is the problem here. Here is my Player script.

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;
    }
}}

Here is the screenshot for hierarchy view.

enter image description here

\$\endgroup\$
1
\$\begingroup\$

It's not clear if the problem is with your source code, but it could actually be an issue with how your scene is arranged. If you want to move the GameObject the GvrViewer script is attached to, you need to create a parent object, and move the parent object instead.

The GvrViewer script has functionality for changing its position and rotation based on the "head" of the user. While Google Cardboard does not have the 3 degrees of positional tracking, the script still modifies the position of the GameObject. In your case, it makes the GameObject stay in one spot. This counteracts all of the movement you're trying to apply to the GameObject.

So create something like this:

enter image description here

And make sure your movement script references the Player object as the one it adjusts the transform of.

\$\endgroup\$
  • \$\begingroup\$ I will try what you are saying. But if I run a normal build without enabling vr mode then everything works fine \$\endgroup\$ – Vivek Mishra Jun 23 '17 at 5:12
  • \$\begingroup\$ I tried what you said but it didn't make any difference. I have also uploaded screenshot of hierarchy view. \$\endgroup\$ – Vivek Mishra Jun 23 '17 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.