4

I am Trying to make a GUI for HTC VIVE but having trouble in opening it on certain controller angle.

I have done some work and achieved a bit sketchy one because my object is a child which make it hard for me to track its rotation or position, as i wanted it to open only when controller is at certain angle (as a guy looking at his watch)

Here is some visual Example:

This is my controller rotation without GUI: Normal GUI closed

As i rotate the controller the GUI should show something like this: at angle (as a guy looking at his watch) GUI Open

Here is some code I have managed

   void RayCastFromHead() // is just a  name for Method i am raycasting from a dummy which contains left Grip button
    {
        if (Physics.Raycast(dummy.position, dummy.up, out hitInfo, 30))
        {
             transform.rotation.ToAngleAxis(out tempAngle, out tempAxis);

            if (hitInfo.collider.name.Contains("Camera (eye)"))
            {
                if (dummy.gameObject.GetComponent<MeshRenderer>().enabled)
                {
                        if ((transform.localEulerAngles.z > 270.0f && transform.localEulerAngles.z < 315.0f)&&
                            (transform.position.y > 0.9f && transform.position.y < 2f))

                        {
                            staticRotaion = transform.localRotation;
                            canvasOnHead.GetComponent<TweenScale>().PlayForward();

                        }
                }
            }
        }

    }

I do not know that it is a right method to do this kind of task? In Simple manner i want to show GUI on certain controller rotation.

This is My hierarchy what i am talking about

This is My hierarchy

This is the same i wanna do with my GUI it should open when my hand angle is something like this image enter image description here

Syed Anwar Fahim
  • 306
  • 5
  • 15
  • plz people help me in this i am really unable to find any solution suitable to my needs – Syed Anwar Fahim Jan 23 '17 at 06:48
  • I think you can just ask Renderer.IsVisible instead of raycasting, should be way more easier. – B4lto Jan 26 '17 at 15:09
  • @Balto I can iopen it easily that not the issue my mian concern is that i am unable to open my gui on certain Controller /Hand angle like the last image above – Syed Anwar Fahim Jan 27 '17 at 12:26
  • **Following answer is not right the question i asked is different** i dont know why people up vote an unaccepted answer this is not the solution i am looking for sorry for Stack Community But no help – Syed Anwar Fahim Jan 31 '17 at 10:21

1 Answers1

2

There is a simple solution to handle UI rotation.

I suppose you have a canvas for your GUI. This canvas can be child of any object. If you add the canvas (root of this menu) as a child of the left hand it should move and rotate with the left hand.

Note that the render mode of the canvas must be World Space.

This is the parent (left hand):

enter image description here

set the values of canvas's rect transform correctly (most important part is pos.z, I changed the scale of the canvas instead of changing the z. I could change width and height of canvas but it would have adverse effects)

enter image description here

it will behave as you described when rotating the parent object (left hand):

enter image description here

Edit

Add this script to your camera

public class LookAtWatchController : MonoBehaviour
{
    public GameObject menuGUI;
    public GameObject hand;
    void Update(){
          if(transform.eulerAngles.x > 10)
          {
                menuGUI.transform.eulerAngles = new Vector3(transform.eulerAngles.x, 0, 0);
                menuGUI.SetActive(true);
                menuGUI.transform.position = hand.transform.position;
          }
          else{
                menuGUI.SetActive(false);
          }
     }
}

assign the gui menu to menuGUI

assign the left hand to hand

you can also include the rotation elements of the hand in the menuGUI rotation:

menuGUI.transform.eulerAngles = new Vector3(transform.eulerAngles.x, hand.transform.eulerAngles.y, hand.transform.eulerAngles.z);

I haven't tested this yet but menuGUI.transform.eulerAngles = new Vector3(transform.eulerAngles.x, 0, 0); should also work fine.

As you see below the rotation.eulerAngles.x of camera and canvas are the same when canvas is seen right in front of camera

enter image description here

enter image description here

Bizhan
  • 16,157
  • 9
  • 63
  • 101
  • I am able to achieve what your are telling me here, my GUI is fine but i want to make it Visible/Open when i turn my controller 180 degree in front of my eyes (here is my problem) **I am unable to make my GUI Visible/Open on a certain controller turn (as a Watch on your hand)** One can only See the watch if the angle of you hand is in front of your eyes – Syed Anwar Fahim Jan 26 '17 at 04:37
  • Oh sorry I completely misunderstood your question. is the camera always looking in forward direction of the player? (only rotates up and down relative to controller). If so, the menu should be opened when the camera angle is slightly downward. yes? – Bizhan Jan 26 '17 at 10:52
  • Yes Camera Always looking at forward but the rotation is not relative to controller, but Camera Rig which is the top most parent of Camera and controllers I am casting a ray from controller in order to find the camera angle, when the ray hits camera then i check for the controller rotation to be exact for GUI to appear – Syed Anwar Fahim Jan 26 '17 at 11:34
  • I'm not sure if I understood the relations, can you add a screenshot of the hierarchy? – Bizhan Jan 26 '17 at 11:58
  • I have applied you Edits but what happening is that GUI is Opening when my head is my hand is in front of each other i did that before to with my own set up but the issue here is the same i asked first **GUI open when i turn my hand/controller to 180 deg or a man looking at his watch with is hand in at certain angle** Please look at the question for example – Syed Anwar Fahim Jan 27 '17 at 06:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134148/discussion-between-syed-anwar-fahim-and-bijan). – Syed Anwar Fahim Jan 27 '17 at 06:51