始终在玩家面前显示对象 [英] Show object in front of the player always

查看:55
本文介绍了始终在玩家面前显示对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了这个简单的问题,但无法理解为什么我无法控制它.

我有这些代码行,它们在播放器一定旋转时将画布对象显示在播放器前面(代码中的 camRotationToWatch 对象名称).

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

camRotationToWatch 对象从向下旋转到上,并且Canvas显示在正确位置时,此代码实际上可以正常工作,但是当我尝试旋转 camRotationToWatch 时, strong>(从向上到向下)在最高位置显示(活动)画布.如何限制画布显示在相同位置(无论玩家从上向下旋转还是从下向上旋转)但显示在玩家对象的前面?

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努力弄清楚您到底想做什么.但这确实做到了我认为您要尝试的地方

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

我相信您的意外行为"是由于使用了欧拉角,因为它们并不总是可以完全预测的.尽可能尝试使用四元数或Vector3.Angle().

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.

如果要限制角度(例如...如果向下或向上倾斜超过45°,则禁用该对象),可以执行以下操作:

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) { ... }

这篇关于始终在玩家面前显示对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆