Implementing a "flyer" game in with the old CardBoard Google sdk
Hello everyone,
As a homework project, I've set about creating a flyer like game such as in the VR Samples provided by Unity: https://assetstore.unity.com/packages/essentials/tutorial-projects/vr-samples-51519
However, I am not able to translate the FlyeMovementController script in this package to the old pre 0.8 Google Cardboard sdk which I am expected to use.*
Should I use the "Head" Gaze and Vector3.Lerp to make the ship move to the desired location? I can't seem to make it work.
The script in question:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.VR;
using VRStandardAssets.Common;
namespace VRStandardAssets.Flyer
{
// This class handles the movement of the flyer ship.
public class FlyerMovementController : MonoBehaviour
{
[SerializeField] private float m_DistanceFromCamera = 75f; // The distance from the camera the ship aims to be.
[SerializeField] private float m_Speed = 100f; // The speed the ship moves forward.
[SerializeField] private float m_Damping = 0.5f; // The amount of damping applied to the movement of the ship.
[SerializeField] private Transform m_Flyer; // Reference to the transform of the flyer.
[SerializeField] private Transform m_TargetMarker; // The transform the flyer is moving towards.
[SerializeField] private Transform m_Camera; // Reference to the camera's transform.
[SerializeField] private Transform m_CameraContainer; // Reference to the transform containing the camera.
[SerializeField] private Text m_CurrentScore; // Reference to the Text component that will display the user's score.
private bool m_IsGameRunning; // Whether the game is running.
private Vector3 m_FlyerStartPos; // These positions and rotations are stored at Start so the flyer can be reset each game.
private Quaternion m_FlyerStartRot;
private Vector3 m_TargetMarkerStartPos;
private Quaternion m_TargetMarkerStartRot;
private Vector3 m_CameraContainerStartPos;
private const float k_ExpDampingCoef = -20f; // The coefficient used to damp the movement of the flyer.
private const float k_BankingCoef = 3f; // How much the ship banks when it moves.
private void Start ()
{
// Store all the starting positions and rotations.
m_FlyerStartPos = m_Flyer.position;
m_FlyerStartRot = m_Flyer.rotation;
m_TargetMarkerStartPos = m_TargetMarker.position;
m_TargetMarkerStartRot = m_TargetMarker.rotation;
m_CameraContainerStartPos = m_CameraContainer.position;
}
public void StartGame ()
{
// The game is now running.
m_IsGameRunning = true;
// Start the flyer moving.
StartCoroutine (MoveFlyer ());
}
public void StopGame ()
{
// The game is no longer running.
m_IsGameRunning = false;
// Reset all the positions and rotations that were store.
m_Flyer.position = m_FlyerStartPos;
m_Flyer.rotation = m_FlyerStartRot;
m_TargetMarker.position = m_TargetMarkerStartPos;
m_TargetMarker.rotation = m_TargetMarkerStartRot;
m_CameraContainer.position = m_CameraContainerStartPos;
}
private IEnumerator MoveFlyer ()
{
while (m_IsGameRunning)
{
// Set the target marker position to a point forward of the camera multiplied by the distance from the camera.
Quaternion headRotation = FauxInputTracking.GetLocalRotation (VRNode.Head);
m_TargetMarker.position = m_Camera.position + (headRotation * Vector3.forward) * m_DistanceFromCamera;
// Move the camera container forward.
m_CameraContainer.Translate (Vector3.forward * Time.deltaTime * m_Speed);
// Move the flyer towards the target marker.
m_Flyer.position = Vector3.Lerp(m_Flyer.position, m_TargetMarker.position,
m_Damping * (1f - Mathf.Exp (k_ExpDampingCoef * Time.deltaTime)));
// Calculate the vector from the target marker to the flyer.
Vector3 dist = m_Flyer.position - m_TargetMarker.position;
// Base the target markers pitch (x rotation) on the distance in the y axis and it's roll (z rotation) on the distance in the x axis.
m_TargetMarker.eulerAngles = new Vector3 (dist.y, 0f, dist.x) * k_BankingCoef;
// Make the flyer bank towards the marker.
m_Flyer.rotation = Quaternion.Lerp(m_Flyer.rotation, m_TargetMarker.rotation,
m_Damping * (1f - Mathf.Exp (k_ExpDampingCoef * Time.deltaTime)));
// Update the score text.
m_CurrentScore.text = "Score: " + SessionData.Score;
// Wait until next frame.
yield return null;
}
}
}
}
I have seen the video by @Selzier NUrFACE: https://www.youtube.com/watch?v=RnAEO-QlqSg talking about how to change the samples to the new sdk with the cardboard main, but when I try this with GvrMain, I can't interact or move the head.
I'm using Unity 5.6.31f with Google SDK 0.8
Answer by Selzier · Oct 14, 2017 at 03:54 PM
Can you try just replacing this line:
Quaternion headRotation = FauxInputTracking.GetLocalRotation (VRNode.Head);
with the head rotation from cardboard SDK, in 0.6 that would be Cardboard.SDK.HeadPose.Orientation:
Quaternion headRotation = Cardboard.SDK.HeadPose.Orientation;
Look at the child of the main "CardboardMain" or "GoogleVRMain" and see what script is on that gameobject, it should be a "head" script. Get the orientation variable from this script from head rotation.
This video might show more info: https://www.youtube.com/watch?v=zmpZojt1vLY
Answer by dorgrin6 · Oct 18, 2017 at 03:00 PM
Just updating for the sake of others, on my version of the cardboard sdk there is no Cardboard.SDK (or at least I couldn't find it).
I ended up using the camera local rotation instead: i.e. I changed the line
Quaternion headRotation = InputTracking.GetLocalRotation (VRNode.Head);
to
Quaternion headRotation = m_camera.localRotation;
with a few minor editor tweaks this works ok.
Thanks for the help @Selzier.
Your answer
Follow this Question
Related Questions
Lerping Camera Between Two Points 0 Answers
Why isn't my object lerping ? C# 2 Answers
Move the character automaticly with the road 0 Answers
Navigation in VR Mode?!! 0 Answers
help with player movement 0 Answers