Search Unity

Google VR : Camera controller script confused

Discussion in 'AR/VR (XR) Discussion' started by Deleted User, Oct 5, 2016.

  1. Deleted User

    Deleted User

    Guest

    Hi,

    I wrote a code to move the "Main Camera" using the up, down, right, left keys for the new update of Google VR SDK. I used the code Autowalk.cs that worked with the older version of Cardboard Unity Master, but today when I simply orients the camera without touching the keys, the camera starts moving by itself with no way to stop it.

    So maybe I didn't write the code well. Do you have any where this problem comes from and how I could solve it ? Do you have any idea ?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class walking_camera : MonoBehaviour
    5. {
    6.  
    7.     // This variable determinates if the player will move or not
    8.     private bool isWalkingup = false;
    9.     private bool isWalkingdown = false;
    10.     private bool isWalkingright = false;
    11.     private bool isWalkingleft = false;
    12.  
    13.     Camera head = null;
    14.  
    15.     //La variable permettant de choisir la vitesse de deplacement
    16.     [Tooltip("Renseigner la vitesse de deplacement de la camera")]
    17.     public float speed;
    18.  
    19.     [Tooltip("Activer cette case pour bloquer l'axe y de la camera")]
    20.     public bool freezeYPosition;
    21.  
    22.     [Tooltip("Renseigner l'axe y de la camera")]
    23.     public float yOffset;
    24.  
    25.     void Start ()
    26.     {
    27.         head = Camera.main;
    28.     }
    29.  
    30.     void Update ()
    31.     {
    32.         // FORWARD
    33.  
    34.         if (Input.GetKey("up"))
    35.         {
    36.             isWalkingup = true;
    37.         }
    38.         else if (!Input.GetKey("up"))
    39.         {
    40.             isWalkingup = false;
    41.         }
    42.  
    43.         if (isWalkingup)
    44.         {
    45.             Vector3 direction = new Vector3(head.transform.forward.x, 0, head.transform.forward.z).normalized * speed * Time.deltaTime;
    46.             Quaternion rotation = Quaternion.Euler(new Vector3(0, -transform.rotation.eulerAngles.y, 0));
    47.             transform.Translate(rotation * direction);
    48.         }
    49.  
    50.         // REARWARD
    51.  
    52.         if (Input.GetKey("down"))
    53.         {
    54.             isWalkingdown = true;
    55.         }
    56.         else if (!Input.GetKey("down"))
    57.         {
    58.             isWalkingdown = false;
    59.         }
    60.      
    61.         if (isWalkingdown)
    62.         {
    63.             Vector3 direction = new Vector3(-head.transform.forward.x, 0, -head.transform.forward.z).normalized * speed * Time.deltaTime;
    64.             Quaternion rotation = Quaternion.Euler(new Vector3(0, -transform.rotation.eulerAngles.y, 0));
    65.             transform.Translate(rotation * direction);
    66.         }
    67.  
    68.         // TO RIGHT
    69.      
    70.         if (Input.GetKey("right"))
    71.         {
    72.             isWalkingright = true;
    73.         }
    74.         else if (!Input.GetKey("right"))
    75.         {
    76.             isWalkingright = false;
    77.         }
    78.      
    79.         if (isWalkingright)
    80.         {
    81.             Vector3 direction = new Vector3(head.transform.forward.z, 0, -head.transform.forward.x).normalized * speed * Time.deltaTime;
    82.             Quaternion rotation = Quaternion.Euler(new Vector3(0, -transform.rotation.eulerAngles.y, 0));
    83.             transform.Translate(rotation * direction);
    84.         }
    85.  
    86.         // TO LEFT
    87.      
    88.         if (Input.GetKey("left"))
    89.         {
    90.             isWalkingleft = true;
    91.         }
    92.         else if (!Input.GetKey("left"))
    93.         {
    94.             isWalkingleft = false;
    95.         }
    96.      
    97.         if (isWalkingleft)
    98.         {
    99.             Vector3 direction = new Vector3(-head.transform.forward.z, 0, head.transform.forward.x).normalized * speed * Time.deltaTime;
    100.             Quaternion rotation = Quaternion.Euler(new Vector3(0, -transform.rotation.eulerAngles.y, 0));
    101.             transform.Translate(rotation * direction);
    102.         }
    103.  
    104.         if(freezeYPosition)
    105.         {
    106.             transform.position = new Vector3(transform.position.x, yOffset, transform.position.z);
    107.         }
    108.     }
    109. }


    In this video when I press the down key then the movement is like laggy.

    Thank you in advance for your help !
     
    Last edited by a moderator: Oct 5, 2016