In unity, the model automatically rotates

1. In the project, sometimes it is necessary to display the product and let the model rotate automatically

The following code is the automatic rotation of the model

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

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

2. There is automatic rotation, there must be manual rotation to view the model

The following code will show the manual rotation of the model

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;
        }
    }

}

Guess you like

Origin blog.csdn.net/qq_46641769/article/details/126603787