简体   繁体   English

Unity:面对相机时无法锁定对象的z轴

[英]Unity: Can't get object's z axis to lock when facing camera

I want a sprite to always face the camera, except in the Z-Axis.我想要一个精灵总是面对相机,除了 Z 轴。 My sprites keep tipping left or right when I move the camera, and I can't have that.当我移动相机时,我的精灵不断向左或向右倾斜,而我不能这样做。

I have been googling this for hours.我已经在谷歌上搜索了几个小时。 I've tried transform.LookAt or Quaternion.LookRotation and manually setting the z to 0, but for whatever reason the z keeps adjusting.我试过 transform.LookAt 或 Quaternion.LookRotation 并手动将 z 设置为 0,但无论出于何种原因 z 不断调整。 I've seen and tried so many solutions that feel like they should work but just don't.我已经看到并尝试了很多解决方案,感觉它们应该起作用,但实际上却没有。 If it matters, my sprite is a child of another object, but trying localRotation doesn't work either.如果重要的话,我的精灵是另一个对象的子对象,但尝试 localRotation 也不起作用。 Freezing rigidbody constraints also has no effect.冻结刚体约束也无效。

The most accurate I can get it is this:我能得到的最准确的是:

public class Billboard : MonoBehaviour
{
    GameObject cam;
    float minDist;
    // Start is called before the first frame update
    void Start()
    {
        cam = GameObject.Find("Main Camera");
    }

    // Update is called once per frame
    void LateUpdate()
    {
        //Scale
        minDist = cam.GetComponent<CameraOrbit>().distanceMin;
        transform.localScale = new Vector3(1f, 1f, 1f) * (cam.GetComponent<CameraOrbit>().distance - minDist) * 1.01f / 3;

        //Direction
        transform.LookAt(cam.transform.position);
        Vector3 rot = transform.rotation.eulerAngles;
        transform.rotation = Quaternion.Euler(rot.x, rot.y, 0);

    }
}

With this I can get the sprite to face the camera, but the z axis refuses to stay at zero.有了这个,我可以让精灵面对相机,但 z 轴拒绝保持为零。

Special thanks to StarManta for answering this question on the Unity Forums.特别感谢 StarManta 在 Unity 论坛上回答这个问题。

"OK, I think I see what's happening. Them looking like they're rotated is, I think, an illusion; I think they really are accurately pointing at the camera and right-side-up. But, you're treating the camera as if it has a round/fisheye projection, when it really has a rectilinear projection. Usually this doesn't matter but in this case it does. It's hard to explain exactly how this affects this, but the upshot is that, when things that are on either side of the center of the screen are set to "look towards" the camera, they actually appear to be rotated around their Y axes. “好吧,我想我明白发生了什么。他们看起来像是在旋转,我认为是一种错觉;我认为他们确实准确地指向相机并且正面朝上。但是,你正在对待相机好像它有一个圆形/鱼眼投影,当它真的有一个直线投影时。通常这无关紧要,但在这种情况下确实如此。很难确切解释这如何影响这一点,但结果是,当事情发生时在屏幕中心的任一侧设置为“朝向”相机,它们实际上似乎围绕它们的 Y 轴旋转。

The solution to this is actually annoyingly simple: don't set the rotation to look at the camera, set it to be the same as the camera."解决这个问题的方法其实很简单:不要设置旋转角度看相机,设置它与相机相同。”

transform.rotation = cam.transform.rotation;

I had a similar problem, where I wanted some 3d text to always look up, but rotated to the camera.我有一个类似的问题,我想要一些 3d 文本总是向上看,但旋转到相机。
What worked for me was setting the undesired euler components to zero after applying the lookat rotation.对我有用的是在应用观察旋转后将不需要的欧拉分量设置为零。
In your case, it would be something like this.在你的情况下,它会是这样的。

transform.LookAt(cam.transform.position);
var rot = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(rot.x, rot.y, 0);

In case you need another z value, just replace the 0.如果您需要另一个 z 值,只需替换 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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