在unity中,模型自动旋转

一. 在项目中,有时候会需要产品展示,并且让模型自动旋转

下面的代码就是模型自动旋转

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

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

二. 有自动旋转,肯定有手动旋转查看模型

下面代码将展示模型的手动旋转

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