Show object in front of player always

I was stuck in this simple problem, but could not understand why I can not control it.

I have this line of code that shows my canvas object in front of my player ( camRotationToWatch is the name of the object in the code) with a 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;
            }
        }

, camRotationToWatch , Canvas , camRotationToWatch strong > . () . , ( , ), ?

+4
2

, . , ,

public GameObject follow; // The object you want to rotate around
public float distance = 2; // Distance to keep from object

private void Update() {
    Vector3 forward = follow.transform.forward;
    forward.y = 0; // This will result in Vector3.Zero if looking straight up or down. Carefull

    transform.position = follow.transform.position + forward * distance;
    transform.rotation = Quaternion.LookRotation(forward, Vector3.up);
}

, " " , . Quaternions Vector3.Angle(), .

(... 45 ° ), :

if (Vector3.Angle(forward, follow.transform.forward) > maxAngle) { ... }
+2

, , , , . :

Vector3 navCanvas = camRotationToWatch.transform.position + camRotationToWatch.transform.forward * navCanvasDisplayDistanceFromCam;

camRotationToWatch. , navCanvasDisplayDistanceFromCam. , .

, , :

navCanvas = new Vector3(navCanvas.x, 2f, navCanvas.z);

camRotationToWatch, , , . , camRotationToWatch.transform.forward, Y- , . ( ). , , , .

EDIT: , :

Vector3 camForward = camRotationToWatch.transform.forward;
camForward.y = 0;
if (camForward.magnitude == 0)
{
    //TODO: you'll need to decide how to deal with a straight up or straight down vector
}
camForward.Normalize();
//Note: now you have a vector lying on the horizontal plane, pointing in
//the direction of camRotationToWatch
Vector3 navCanvas = camRotationToWatch.transform.position + camForward * 
    navCanvasDisplayDistanceFromCam;
//Note: if you want the canvas to be at the player height (or some offset from), 
//use the player y
navCanvas = new Vector3(navCanvas.x, Player.transform.y, navCanvas.z);
navigationCanvas.transform.position = navCanvas;

, , , , .

0

Source: https://habr.com/ru/post/1674468/


All Articles