繁体   English   中英

Unity3D c# 改变对象的旋转

[英]Unity3D c# change rotation of object

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

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. 哦... SunRot 改变了...所以将 SunRot 值复制到 Sun 游戏对象

当我使用 EulerAngles 时,它正在复制方位角 [from server: 304.00075, in transform.rotation: 65.99825] ...我想像 [from server: 304.00075, in transform.rotation: 304.00075]

最好保留来自服务器的先前值,并在更改时使用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 ,除非您偏移了太阳对象的中心,根据您的情况,这可能是理想的,也可能不是理想的。

暂无
暂无

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

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