简体   繁体   中英

Show object in front of the player always

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?

Kinda hard trying to figure out what exactly you want to do. But this did what I think you where trying to do

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);
}

I believe your "unexpected behavior" is due to the use of euler angles since they are not always entirely predictable. Try using Quaternions or Vector3.Angle() when possible.

If you want to limit the angle (say... if looking down or up more than 45° disable the object) you could do the following:

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

This probably isn't a complete answer but something that might help. This line:

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

You are creating a position at a fixed distance from camRotationToWatch . But if that object is looking up or down, that position is not horizontally at navCanvasDisplayDistanceFromCam . If it's looking straight up, then this position is in fact directly above.

So when you do this to set a fixed vertical height:

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

you aren't getting the distance from camRotationToWatch that you think you are. Instead of using camRotationToWatch.transform.forward , create a vector from it and zero out the Y component, and normalize before using it to offset the position. (You will need to watch out for zero length vectors with that though). Whether that fixes your problem or not, it's too hard to guess but it should help improve your results some.

EDIT: Here is an example of how you can avoid the issue with the canvas being too close:

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's height (or some offset from), 
//use the player's y
navCanvas = new Vector3(navCanvas.x, Player.transform.y, navCanvas.z);
navigationCanvas.transform.position = navCanvas;

Again, this might not fix all your issues but will help to ensure your canvas lies at a set distance horizontally from the Player and will also compensate for the player's up and down motion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM