Unity通过鼠标对物体进行旋转、缩放

using UnityEngine;

public class ModelComponent : MonoBehaviour
{
    
    
    [SerializeField] private Camera m_Camera;
    // Update is called once per frame
    void Update()
    {
    
    
        //如果鼠标左键按下
        if (Input.GetMouseButton(0))
        {
    
    
            float speed = 2.5f;//旋转跟随速度
            float OffsetX = Input.GetAxis("Mouse X");//获取鼠标x轴的偏移量
            float OffsetY = Input.GetAxis("Mouse Y");//获取鼠标y轴的偏移量
            transform.Rotate(new Vector3(OffsetY, -OffsetX, 0) * speed, Space.World);//旋转物体
        }
        
        if (Input.GetAxis ("Mouse ScrollWheel") < 0)
        {
    
    
            if (m_Camera == null) return;
            if (m_Camera.fieldOfView <= 80)
                m_Camera.fieldOfView += 2;
           
        }
        if (Input.GetAxis ("Mouse ScrollWheel") > 0)
        {
    
    
            if (m_Camera == null) return;
            if (m_Camera.fieldOfView >= 20)
                m_Camera.fieldOfView -= 2;
      
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41743629/article/details/120784439