如何停止我的Z轴发生180度旋转。

huangapple go评论60阅读模式
英文:

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

当我完全绕一圈时:
如何停止我的Z轴发生180度旋转。

英文:

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

}`

Results:
如何停止我的Z轴发生180度旋转。

When I do a full loop:
如何停止我的Z轴发生180度旋转。

答案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.

huangapple
  • 本文由 发表于 2023年2月7日 04:08:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定