0
\$\begingroup\$

I have stuck in this simple problem but unable to understand that why i am unable to control it.

I have these line of code which is displaying my canvas object in front of my player(camRotationToWatch object name in code) at certain rotation of the player.

if (camRotationToWatch.transform.localEulerAngles.x >= navigationCanvasXMinmumLimit && camRotationToWatch.transform.localEulerAngles.x <= navigationCanvasXMaximumLimit)
        {
            if (!navCanvasHasDisplay) 
            {

                navigationCanvas.SetActive(true);
                //Debug.Log(camRotationToWatch.transform.forward);
                Vector3 navCanvas = camRotationToWatch.transform.position + camRotationToWatch.transform.forward * navCanvasDisplayDistanceFromCam;
                navCanvas = new Vector3(navCanvas.x, 2f, navCanvas.z);
                navigationCanvas.transform.position = new Vector3(navCanvas.x, navCanvas.y, navCanvas.z);

                navigationCanvas.transform.rotation = camRotationToWatch.transform.rotation;

                navCanvasHasDisplay = true;

            }
        }

        else
        {
            //navigationCanvas.SetActive(false);
            if (locationPanel.activeSelf == false && infoPanel.activeSelf == false) {
                navigationCanvas.SetActive(false);
                navCanvasHasDisplay = false;
            }
        }

This code is actually work fine when camRotationToWatch object rotate from down to up and Canvas show at correct position but as I try to to rotate camRotationToWatch from up to down it display(active) Canvas at very top position. How can I restrict canvas to show at same position (No matter player rotate from up to down or down to up) but display on front of the player object?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I believe the problem is that camRotationToWatch.transform.localEulerAngles.x may represent the angle in a way you are not expecting. For example, -30 degrees is the same as +330 degrees.

I'll take a guess that you are expecting a negative angle when looking up, while Unity is returning a positive angle. Try replacing the first line of the code you pasted with this code:

float xFromCamera = camRotationToWatch.transform.localEulerAngles.x;
if (xFromCamera >= 180.0f) xFromCamera -= 360.0f;
if (xFromCamera < -180.0f) xFromCamera += 360.0f;

if (xFromCamera >= navigationCanvasXMinmumLimit && xFromCamera <= navigationCanvasXMaximumLimit)

(Note: I wasn't confident at first, so I decided to test this out in Unity. I set the x rotation of a transform to a negative angle in code, then read the same x rotation later. The returned angle was positive, 360 + my angle.)

\$\endgroup\$
13
  • \$\begingroup\$ i am much frustated just copy your code and now my canvas object not enabling even \$\endgroup\$ Commented Apr 10, 2017 at 15:16
  • \$\begingroup\$ I'm sorry to hear that. Frustration is an unpleasant part of programming. To find the bug, we need more information. Remove my change, and try using Debug.Log to print out the value of camRotationToWatch.transform.localEulerAngles.x. Perhaps examining the numbers that are printed out will tell you what is wrong. \$\endgroup\$
    – Victor T.
    Commented Apr 10, 2017 at 15:30
  • \$\begingroup\$ I am frustated because i am trying to solve this issue from last 4 to 6 hours and still not solve yet. additionally i think that you did n't get the question \$\endgroup\$ Commented Apr 10, 2017 at 15:35
  • \$\begingroup\$ I want to show my canvas at specific position but on in front of my player object. The canvas always show in front of the player but its postion should remain same. No matter i look at up or down.it should show in front of player but not move up and down \$\endgroup\$ Commented Apr 10, 2017 at 15:36
  • \$\begingroup\$ So, while the canvas is visible, you want it to remain in the same position on-screen the whole time? \$\endgroup\$
    – Victor T.
    Commented Apr 10, 2017 at 15:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .