Unity ではモデルが自動的に回転します

1. プロジェクトでは、製品を表示し、モデルを自動的に回転させる必要がある場合があります。

次のコードはモデルの自動回転です。

public class AutoRotationModel : MonoBehaviour
{
    
    
    public float speed = 25;

    void Update()
    {
    
    
        transform.Rotate(0, speed * Time.deltaTime, 0, Space.Self);
    }
}

2. 自動回転がありますが、モデルを表示するには手動回転が必要です

次のコードは、モデルの手動回転を示します。

public class RotateTest : MonoBehaviour
{
    
    
    float h;
    float v;
    float Xspeed = 20.0f;
    float Yspeed = 1.0f;
    public float xMin;
    public float xMax;
    public float distance = 10.0f;
    public Camera cam;
    public bool isVectore = true;
    public float camSpeed;
    public bool isRotate = false;

    public bool X;
    public bool Y;
    public bool Z;

    void Start()
    {
    
    
        cam = GameObject.Find("Camera").GetComponent<Camera>();
    }

    void Update()
    {
    
    
        if (Input.GetMouseButton(0))
        {
    
    
            if (!isRotate)
            {
    
    
                return;
            }

            h = Input.GetAxis("Mouse X");
            v = Input.GetAxis("Mouse Y");
            if (Mathf.Abs(h) >= Mathf.Abs(v))
            {
    
    
                if (h < 0)
                {
    
    
                    transform.Rotate(0, -Time.deltaTime * Xspeed * 100.0f * -h, 0);
                }

                if (h > 0)
                {
    
    
                    transform.Rotate(0, Time.deltaTime * Xspeed * 100.0f * h, 0);
                }
            }
            else
            {
    
    
                if (!isVectore)
                {
    
    
                    return;
                }

                if (v < 0)
                {
    
    
                    transform.Rotate(Time.deltaTime * Yspeed * 100.0f * v, 0, 0, Space.World);
                }

                if (v > 0)
                {
    
    
                    transform.Rotate(-Time.deltaTime * Yspeed * 100.0f * -v, 0, 0, Space.World);
                }
            }
        }
    }

    bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
    {
    
    
        float leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        float leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
        if (leng1 < leng2)
        {
    
    
            return true;
        }
        else
        {
    
    
            return false;
        }
    }

}

おすすめ

転載: blog.csdn.net/qq_46641769/article/details/126603787