Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

VR movement in Unity

Discussion in 'VR' started by KenniKure, Aug 7, 2019.

  1. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    16
    Hi forum.

    I tried to follow this tutorial on youtube to be able to walk through my animation in Unity.
    I followed this tutorial on how to:


    Unfortunately it does not work and I am thrown this error:
    UnassignedReferenceException: The variable vrCamera of VRLookWalk has not been assigned.
    You probably need to assign the vrCamera variable of the VRLookWalk script in the inspector.

    How can I fix it? What am I doing wrong? Can somebody please tell me what I should type instead?
    My code looks like the one in the tutorial, but maybe I have missed something, eventhough I have doublechecked many times.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class VRLookWalk : MonoBehaviour {
    public Transform vrCamera;

    public float toggleAngle = 30.0f;

    public float speed = 3.0f;

    public bool moveForward;

    private CharacterController cc;

    // Start is called before the first frame update
    void Start (){
    cc = GetComponent<CharacterController>();
    }
    // Update is called once per frame
    void Update () {
    if (vrCamera.eulerAngles.x >= toggleAngle && vrCamera.eulerAngles.x < 90.0f){
    moveForward = true;
    }
    else {
    moveForward = false;
    }

    if (moveForward) {
    Vector3 forward = vrCamera.TransformDirection(Vector3.forward);

    cc.SimpleMove(forward * speed);
    }
    }
    }

    Regards

    Kenni
     
  2. stevetsay

    stevetsay

    Joined:
    Aug 7, 2019
    Posts:
    6
    I think your script is fine, maybe you should check your inspector, the "vrCamera" need to be assigned. (Checkout at the tutorial video @4:35)
     
  3. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    16
    upload_2019-8-7_9-47-27.png

    On the right is what my inspector looks like. I dont know what I should change? Where do I assign the vrCamera?
     
  4. stevetsay

    stevetsay

    Joined:
    Aug 7, 2019
    Posts:
    6
    Please check attached image.
     

    Attached Files:

  5. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    16
    Thanks :)

    I have dragged it there, but I still cant move. It is called camera in the left pane, so instead of calling it vrCamera in the code, should I then change it to camera?
     
  6. stevetsay

    stevetsay

    Joined:
    Aug 7, 2019
    Posts:
    6
    I think the name of "vrCamera" is not the issue.

    you could try to debug log the vrCamera.eulerAngles.x to check why the "moveForward" not true.
     
  7. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    16
    upload_2019-8-7_10-37-6.png
    this is my entire code so far (now seen in a different windows)

    How do I find the bebug thing you mention? (Sorry, Im new to Unity and not very good at it, and when it comes to coding in it Im lost.)
     
  8. stevetsay

    stevetsay

    Joined:
    Aug 7, 2019
    Posts:
    6
    Try this and run, and check the unity's console window.

    void Update () {

    Debug.Log("vrCamera.eulerAngles.x: "+vrCamera.eulerAngles.x);

    if (vrCamera.eulerAngles.x >= toggleAngle && vrCamera.eulerAngles.x < 90.0f){
    moveForward = true;
    }
    else {
    moveForward = false;
    }
     
  9. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    16
    upload_2019-8-7_10-57-12.png

    Sadly that didnt work :(
     
  10. stevetsay

    stevetsay

    Joined:
    Aug 7, 2019
    Posts:
    6
    Add "Debug.Log" is a debug procedure to verify why it's not work.

    Press Ctrl+Shift+C to open unity's console window and run, then move the camera and check the console output to see the value of eulerAngles.x. The moveForward will be true while the eulerAngles.x is between toggleAngle and 90.0f degrees.
     
  11. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    16

    upload_2019-8-7_12-2-24.png

    upload_2019-8-7_12-2-56.png

    upload_2019-8-7_12-3-40.png

    The numbers are changing as you can see, but still no movement though?
     
  12. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    16
    Could it be because there is something I havent downloaded from the store? Or is it because my script is "greyed out" in the inspector and that does something to it? (the text is not black as the rest)
    upload_2019-8-7_13-53-44.png
     
  13. stevetsay

    stevetsay

    Joined:
    Aug 7, 2019
    Posts:
    6
    While you run the application and move your head(camera) look down, the angle X should be over 30 degrees.
     
  14. KenniKure

    KenniKure

    Joined:
    Aug 7, 2019
    Posts:
    16
    I am. And yet nothing happens