Search Unity

GoogleVR SDK: how to keep rotating GameObject on gazing?

Discussion in 'VR' started by vanhauttebram, Nov 16, 2018.

  1. vanhauttebram

    vanhauttebram

    Joined:
    Nov 16, 2018
    Posts:
    10
    Hello,

    I am stuck with a little problem.

    I have made a 3D cube and set up the GoogleVR SDK so that I'm using an Event Trigger for when the midpoint of the VR-screen enters the cube. When this happens, my custom method "LookAtCube()" is triggered.
    I want it to make that the cube keeps rotating. I can rotate with the transform.Rotate-function, but the problem is that it only rotates for 1 game tick (I think) because the Event I'm triggering on is "Pointer Enter".

    My question: is there an event that you can trigger that keeps executing the given method when being in the distances of the collider? (for example when looking at the cube it should rotate, when not looking it should not rotate).

    I tried to fix this with a while(true)-loop, but the game simply crashes.

    For example on a PC/Standalone application, you can simply use the private method OnMouseOver(). I would like this but then with the focus (midpoint) of the VR screen.

    This is what I tried so far (2 examples):

    First example: it simply rotates a little bit and stops (so I guess it rotates for 1 gametick at the set speed).
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. public class MoveCube : MonoBehaviour
    5. {
    6.     float rotateSpeed = 0.5f;
    7.  
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.     }
    12.  
    13.     public void LookAtCube()
    14.     {
    15.         transform.Rotate(new Vector3(rotateSpeed, rotateSpeed, rotateSpeed));
    16.     }
    17.  
    18.     public void LookOutCube()
    19.     {
    20.         transform.Rotate(new Vector3(0, 0, 0));
    21.     }
    22. }
    Second example: I used a boolean that I set true/false respectively when entering/exiting the cube. Then the while loop should keep it rotating, but the game crashes (can't see the error message since it totally crashes, but I guess an overflowexpection).
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. public class MoveCube : MonoBehaviour
    5. {
    6.     float rotateSpeed = 0.5f;
    7.     Boolean hoverState = false;
    8.  
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.     }
    13.  
    14.     public void LookAtCube()
    15.     {  
    16.         while(hoverState == true){
    17.              transform.Rotate(new Vector3(rotateSpeed, rotateSpeed, rotateSpeed));
    18.         }
    19.     }
    20.  
    21.     public void LookOutCube()
    22.     {
    23.         hoverState = false;
    24.         transform.Rotate(new Vector3(0, 0, 0));
    25.     }
    26. }
    Any ideas on how to implement it correctly? Thanks in advance!
     
  2. vanhauttebram

    vanhauttebram

    Joined:
    Nov 16, 2018
    Posts:
    10