Search code examples
c#androidvirtual-reality

Steering object by head VR Cardboard


Can somebody tell me how to do head based steering? I know how to do it while mooving only camera, but i want to moove object by head and camera to follow the object. I need this because i want to implement game logic to the object(it will aleso work for me if i can treat camera as an normal object).

I found this code for steering only camera:

public float speed = /* some number */;

private CardboardHead head;

void Start()
{
head = // find the CardboardHead
// example:
// head = Camera.main.GetComponent<StereoController>().Head;
// or, make the variable public and use drag-and-drop in the Editor
}

void Update()
{
transform.position += speed * head.Gaze.direction;
}

I'm starting to make games for unity so if I made some mistakes please correct me.


Solution

  • Are you using cardboard plugin for Unity from here? https://developers.google.com/cardboard/unity/download

    if you do, then there is a prefab called cardboard main, put it in the scene, then if you see there is a game object called Head inside it and it contains CardboardHead script. So here is in your script:

    public GameObject head; // drag the Head gameobject to here or you can just get it from the start
    
    void Start()
    {
        if (!head) // if you didn't drag the gameobject
        {
            head = FindObjectOfType<CardboardHead>().gameObject;
        }
    }
    
    void Update()
    {
        transform.position += speed * head.transform.forward; // it is better to multiply by Time.deltaTime
    }
    

    also btw why it is tagged as Java?


    New questions:

    attachment

    So here is what I did. I split the 'visual' and 'mechanic'. I removed the Mesh Renderer from the player, freeze its rotation for the rigidbody. I made a new child for the 'visual' it has a Script called Rotate.cs and removed its collider.

    Changes to script: in Forward.cs use the rb.AddForce(transform.forward * speed); back, and in Rotate.cs just use

    void Update () {
            transform.Rotate(Vector3.right * 5);
    }
    

    That is the quickest solution I can think of, I'm sure there are other solutions which you can try