英文:
How do I stop having my Z axis do a 180 on me
问题
这是我的当前代码:
{
/*此脚本用于在画布上创建广告牌效果,以便它们会在垂直轴(左右)上跟随我的玩家。*/
public Camera _playerCamera;
private void FixedUpdate()
{
transform.LookAt(transform.position + _playerCamera.transform.rotation * Vector3.forward, _playerCamera.transform.rotation.y * Vector3.up);
}
}
英文:
I'm trying to create a billboard effect for my canvas in vr where it would follow my player on the y axis (side to side), for the most part my code works and it only spins around itself when i go around it. However when I do a full loop for some reason my z axis goes up 180 degrees displaying my canvas upside down. How would I fix this problem?
This is my current code
`public class Billboard : MonoBehaviour
{
/Ce script sert a creer un effet billboard sur nos texte, alors il suiveront la camera du joueur/
public Camera _playerCamera;
private void FixedUpdate()
{
transform.LookAt(transform.position + _playerCamera.transform.rotation * Vector3.forward, _playerCamera.transform.rotation.y * Vector3.up);
}
}`
答案1
得分: 1
不要翻译的部分:N/A
翻译好的部分:
看起来你把它过于复杂化了。尝试一些更简单的东西,比如:
transform.LookAt(_playerCamera.transform, _playerCamera.transform.up);
这个函数以一个变换作为目标,所以只需让它直接朝向相机。
如果你只想它在Y轴上旋转,那么可以像这样做:
Vector3 flatOffset = _playerCamera.transform.position - transform.position;
flatOffset.y = 0;
transform.rotation = Quaternion.LookRotation(flatOffset, _playerCamera.transform.up);
另外,这可能更适合放在Update()中,因为它影响需要在每个渲染帧中执行的内容,而不是在FixedUpdate()中,后者更适用于后台模拟等物理类操作,可能会在一个帧内运行多次。
英文:
It looks like you're overcomplicating it. Try something simpler, like:
transform.LookAt(_playerCamera.transform, _playerCamera.transform.up);
That function takes a transform as a target, so just make it look directly at the camera.
If you want it to only rotate on the Y axis, then you can do something like this:
Vector3 flatOffset = _playerCamera.transform.position-transform.position;
flatOffset.y = 0;
transform.rotation = Quaternion.LookRotation(flatOffset, _playerCamera.transform.up);
Also, this would probably be better in Update() since it affects something that needs to be done per rendered frame, rather than in FixedUpdate(), which is more for background simulation stuff like physics and may run more than once per frame.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论