Unity展示鼠标滚动效果

鼠标操作物体旋转做3D展示,以及滚轮滑动远近距离观察功能

以下脚本挂在物体上,把场景中相机给到变量中

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class test : MonoBehaviour
{
    public Camera cam;
    public float speed;
    void Update()
    {
        transform.Rotate(Vector3.up, Space.World);//物体自身一直旋转


        Vector3 fwd = cam.transform.forward;
        fwd.Normalize();
         //利用叉乘得到的向量来旋转
        if (Input.GetMouseButton(0))
        {
            Vector3 vaxis = Vector3.Cross(fwd, Vector3.right);
            transform.Rotate(vaxis, -Input.GetAxis("Mouse X")* speed, Space.World);
            Vector3 haxis = Vector3.Cross(fwd, Vector3.up);
            transform.Rotate(haxis, -Input.GetAxis("Mouse Y")* speed, Space.World);
        }

                if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            Debug.Log("向后滑动");
            if (cam.fieldOfView <= 80) //限制滑动范围
                cam.fieldOfView += 2;
        }
       
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            Debug.Log("向前滑动");
            if (cam.fieldOfView > 25) //限制滑动范围
                cam.fieldOfView -= 2;
        }


    }
 
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!