【问题标题】:Unity3D c# change rotation of objectUnity3D c#改变物体的旋转
【发布时间】:2020-09-11 05:32:58
【问题描述】:

我正在尝试在我的游戏中进行日/夜循环...有一个服务器向我发送太阳方位角,并使用该方位角我想更改定向光的旋转,这是我的代码:

public static Sun instance;
    public GameObject gameObject;

    public Vector3 SunRot;
    private bool rotate;

    void FixedUpdate()
    {
        ///Rotate gameObject with SunRot values
    }

    internal void UpdateRotation(double altitude, double azimuth)
    {
        Debug.Log(azimuth);
        SunRot = new Vector3((float)azimuth, (float)altitude, 0);
        rotate = true;
    }

我的逻辑是:

  1. 调用 UpdateRotation
  2. ooo...SunRot 已更改...所以将 SunRot 值复制到 Sun 游戏对象

当我使用 EulerAngles 时,它会像 [来自服务器:304.00075,在 transform.rotation:65.99825] 中复制方位角...我想这样做 [来自服务器:304.00075,在 transform.rotation:304.00075]

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    最好保留服务器以前的值,并在更改时使用Transform.Rotate(deltaRotation)。不过,您需要小心边界 0-360 的情况。

    public static Sun instance;
        public GameObject gameObject;
        private Transform sunTransform = gameObject.Transform;
        private Vector3 previousServerValues;
        private bool rotate;
    
        void FixedUpdate()
        {
            ///Rotate gameObject with SunRot values
        }
    
        internal void UpdateRotation(double altitude, double azimuth)
        {
            var newServerVals = new Vector3((float)azimuth, (float)altitude, 0); 
            Vector3 deltaRotation = newServerRot - previousServerRot ;
            if (deltaRotation.x < 0)   //boundary case, may need it for altitude as well
                deltaRotation.x += 360;
            sunTransform.Rotate (deltaRotation);
            rotate = true;
        }
    

    顺便说一下,您可能需要使用Transform.RotateAround,除非您偏移了太阳物体的中心,这取决于您的情况,这可能是理想的,也可能不是理想的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      相关资源
      最近更新 更多