簡體   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