2

I have been working on programming a graph in Unity that rotates based on head movement. I have been having multiple issues with the rotation aspect of it.

A sample of the Autowalk class, which I am using to find the angle that the graph needs to rotate based on where the user is facing:

public class AutoWalk : MonoBehaviour { 
//VR head
private Transform vrHead;
//angular displacement from normal
public float xAng, yAng, zAng;
//previous values
public float xOrig, yOrig, zOrig;

// Use this for initialization
void Start () {
//Find the VR head
    vrHead = Camera.main.transform;
    //finding the initial direction
    Vector3 orig = vrHead.TransformDirection(Vector3.forward);
    xOrig = orig.x;
    yOrig = orig.y;
    zOrig = orig.z;
}
// Update is called once per frame
void Update () {
    //find the forward direction
    Vector3 forward = vrHead.TransformDirection(Vector3.forward);
    float xForward = forward.x;
    float yForward = forward.y;
    float zForward = forward.z;
    //find the angle between the initial and current direction
    xAng = Vector3.Angle(new Vector3(xOrig, 0, 0), new Vector3(xForward, 0, 0));
    yAng = Vector3.Angle(new Vector3(0, yOrig, 0), new Vector3(0, yForward, 0));
    zAng = Vector3.Angle(new Vector3(0, 0, zOrig), new Vector3(0, 0, zForward));
    //set new original angle
    xOrig = xAng;
    yOrig = yAng;
    zOrig = zAng;
}

From there I go to the ReadingPoints class, which contains all of the points on the graphs (spheres) and axes (stretched out cubes):

public class ReadingPoints : MonoBehaviour {
// Update is called once per frame
void Update () {
    float xAngl = GameObject.Find("GvrMain").GetComponent<AutoWalk>().xAng;
    float yAngl = GameObject.Find("GvrMain").GetComponent<AutoWalk>().yAng;
    float zAngl = GameObject.Find("GvrMain").GetComponent<AutoWalk>().zAng;
    if ((xAngl != prevXAngl) || (yAngl!=prevYAngl) || (zAngl!=prevZAngl))
    {
        //rotate depending on the angle from normal
        foreach (GameObject o in allObjects)
        {
            o.transform.Rotate(new Vector3(xAngl, yAngl, zAngl), Space.World);
            prevXAngl = xAngl;
            prevYAngl = yAngl;
            prevZAngl = zAngl;
        }
    } 

allObjects, as the name implies, contains the points and the axes.

Anyway, the first issue that is upon running the program is that the graph appears to be torn apart. How the graph is supposed to look (this is what it looks like when o.transform.Rotate(...) is commented out) Here is how it actually looks :( Also, the graph does not rotate when I move, and I have no idea why (I thought I might be using the Rotate function improperly but perhaps I also didn't find the correct angles?). Any ideas of what went wrong are much appreciated, thank you!

egmarkel
  • 45
  • 9

1 Answers1

0

First of all I think you should start using Quaternions (https://docs.unity3d.com/ScriptReference/Quaternion.html) Secondly; while you rotate a object with a Camera attached its line of view will change and eventually the object(Graph) will be out of view. If you want as much of the full graph as possible to remain in view of camera as long as possible. You should give the Quaternion of the graph the opposite rotation of that applied to your Camera or the object it is attached to.

Make sure your all elements of your graph are children of a central gameobject in your graph, and only manipulate that point.

Jeroen De Clercq
  • 403
  • 2
  • 10
  • Thank you for your suggestions, I will have to figure out how to use Quaternions! :) Could you clarify what you mean when you talk about the line of view changing and the graph being out of view? I am a bit confused as to how that happens. – egmarkel Aug 05 '17 at 23:56
  • Unity is game engine; in your play-scene you will always be looking through a camera. Since you talk about moving your head ; i am assuming that the camera rotates identical to the head. When a camera rotates it's line of sight changes. Unless the graph is an overlay it will leave the line of sight at some point during rotations. – Jeroen De Clercq Aug 06 '17 at 10:37