Place a UI element in the x and z direction of the user's gaze, keeping y value "straight ahead"
I want a UI element to pop up when a user has finished my game. It's a Cardboard game and what I want is for the element to pop up ahead of the viewer, in the direction of his gaze, but only for the x and z values.
IT doesn't make sense to follow the user's gaze's Y-value because I don't want the UI element to pop up on the ground if the user is looking down.
So to summarize: element must appear in the x and z direction the user is looking at, but ignore the y value of the gaze. Instead it should pace the UI element at a fixed height in the x,z direction of the gaze.
I tried the following, but what happens is when the gaze looks down, the UI element get generated not under the user (so it works as expected) but straight in front of him (so that works as expected). But it gets generated really close to the user - right in his face.
Here's my code: CardboardHead head = Camera.main.GetComponent().Head; Canvas endGameObject = (Canvas)GameObject.Instantiate (endGameCanvas);
Vector3 adjustedHeadDir = new Vector3 (head.Gaze.direction.x, 0, head.Gaze.direction.z);
Debug.Log (head.Gaze.direction);
Debug.Log ("head " + adjustedHeadDir);
endGameObject.transform.position = adjustedHeadDir*10 + head.transform.position;
Debug.Log (adjustedHeadDir * 10);
endGameObject.transform.LookAt (2*endGameObject.transform.position-head.transform.position);
What I'm not getting is what causes the canvas to show up so close to the user. What causes it to be generated so close to the user when he looks down or up?
What kind of output are you getting from your debug statements?
Would be good to show a look down/forward/up result for each.
@$$anonymous$$mmpies thanks for response.
Attached are 2 screenshots: first one shows meeasurements and debug log when the user faces straight ahead. The placement of the panel is as expected.
Then screenshot 02 shows what happens if the user is looking down. The panel shows up much closer to the head. I'm confused by why, because I expect that by setting y = 0, I'm forcing the direction to be "straight ahead".
Also here is the sample code again- making sure you know where the debugs are called:
private void showGameOverPanel(){
CardboardHead head = Camera.main.GetComponent<StereoController>().Head;
Canvas endGameObject = (Canvas)GameObject.Instantiate (endGameCanvas);
float $$anonymous$$YValue = $$anonymous$$athf.$$anonymous$$ax (0, $$anonymous$$athf.Abs (head.Gaze.direction.y));
Vector3 adjustedHeadDir = new Vector3 (head.Gaze.direction.x, 0, head.Gaze.direction.z);
Debug.Log ("head position " + head.transform.position);
Debug.Log ("direction: " + head.Gaze.direction);
Debug.Log ("adjusted: " + adjustedHeadDir);
Debug.Log ("\n");
endGameObject.transform.position = adjustedHeadDir*10 + head.transform.position;
endGameObject.transform.LookAt (2*endGameObject.transform.position-head.transform.position);
}
Answer by lgorse · Jun 13, 2016 at 03:25 AM
I'm using this hack:
private float minAllowableDirection(float rawValue){
if (Mathf.Abs (rawValue) <= 0.2f) {
if (rawValue >= 0) {
return rawValue + 0.7f;
} else {
return rawValue - 0.7f;
}
} else {
return rawValue;
}
}
Basically when the user's gaze goes very low down the X value goes to 0. Any value in the -0.2 to 0.2 range yield to short a distance. So if the value is that low, I adjust for it with what I consider to be an ugly hack - I'm guessing there's a rule here about vectors that I'm forgetting or not finding in the doc, that will allow me to do a better solution. But I'm not finding it.
I was going to suggest getting a point in the forward direction about half way to the ground (or 1 meter as you might be looking up). From that point raycast directly down to get a point on the terrain.
have an empty gameObject at the players feet and get that to look at the point the raycast hits. Then get a point in that direction forward X meters.
A bit convoluted. The alternative is start recording the head look rotation and only instantiate the menu if the player is looking within a set of angles so they're not looking straight down or up.