Unity:面对相机时无法锁定对象的 z 轴
Unity: Can't get object's z axis to lock when facing camera
我想要一个精灵始终面向相机,Z 轴方向除外。当我移动相机时,我的 sprite 会一直向左或向右倾斜,但我无法做到这一点。
我已经在谷歌上搜索了几个小时。我试过 transform.LookAt 或 Quaternion.LookRotation 并手动将 z 设置为 0,但无论出于何种原因,z 一直在调整。我见过并尝试过很多解决方案,感觉它们应该有效,但就是无效。如果重要的话,我的精灵是另一个对象的子对象,但尝试 localRotation 也不起作用。冻结刚体约束也没有效果。
我能得到的最准确的是:
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);
}
}
有了这个我可以让精灵面对相机,但 z 轴拒绝保持在零。
你试过旋转约束吗?
https://docs.unity3d.com/Manual/class-RotationConstraint.html
我有一个类似的问题,我希望一些 3d 文本始终向上看,但旋转到相机。
对我有用的是在应用观察旋转后将不需要的欧拉分量设置为零。
在你的情况下,它会是这样的。
transform.LookAt(cam.transform.position);
var rot = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(rot.x, rot.y, 0);
如果您需要另一个 z 值,只需替换 0。
特别感谢 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 "看向“相机,它们实际上看起来是绕着Y轴旋转的。
解决这个问题其实很烦人简单:不要设置旋转看摄像头,设置成和摄像头一样。"
transform.rotation = cam.transform.rotation;
我想要一个精灵始终面向相机,Z 轴方向除外。当我移动相机时,我的 sprite 会一直向左或向右倾斜,但我无法做到这一点。
我已经在谷歌上搜索了几个小时。我试过 transform.LookAt 或 Quaternion.LookRotation 并手动将 z 设置为 0,但无论出于何种原因,z 一直在调整。我见过并尝试过很多解决方案,感觉它们应该有效,但就是无效。如果重要的话,我的精灵是另一个对象的子对象,但尝试 localRotation 也不起作用。冻结刚体约束也没有效果。
我能得到的最准确的是:
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);
}
}
有了这个我可以让精灵面对相机,但 z 轴拒绝保持在零。
你试过旋转约束吗?
https://docs.unity3d.com/Manual/class-RotationConstraint.html
我有一个类似的问题,我希望一些 3d 文本始终向上看,但旋转到相机。
对我有用的是在应用观察旋转后将不需要的欧拉分量设置为零。
在你的情况下,它会是这样的。
transform.LookAt(cam.transform.position);
var rot = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(rot.x, rot.y, 0);
如果您需要另一个 z 值,只需替换 0。
特别感谢 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 "看向“相机,它们实际上看起来是绕着Y轴旋转的。
解决这个问题其实很烦人简单:不要设置旋转看摄像头,设置成和摄像头一样。"
transform.rotation = cam.transform.rotation;