[Unity] unity3d通过鼠标滚轮控制视角放大缩小

查看:3763 |回复:269 | 2014-10-10 06:27:01

您需要 登录 才可以下载或查看,没有账号?注册

x
unity3d通过鼠标滚轮控制视角放大缩小,按钮控制放大缩小。
Snipaste_2018-08-12_00-19-29.jpg


using UnityEngine;
using System.Collections;

public class CubeStatic : MonoBehaviour
{
    public GameObject bg;
     
    public float minFov = 15f;
    public float maxFov = 90f;
    public float sensitivity = 10f;
    private float fov;

    public GUIStyle fangdaStyle;
    public GUIStyle suoxiaoStyle;
    private float btnWidth = 64;
    private float btnHeight = 64;
    public GUISkin windowSkin;
    private float screenWidth = Screen.width;
    //private float screenHeight = Screen.height;

    void Start(){
        fov = Camera.main.fieldOfView;
    }
    void Update() {

        fov += Input.GetAxis("Mouse ScrollWheel") * sensitivity;
        fov = Mathf.Clamp(fov, minFov, maxFov);//限制value的值在min和max之间, 如果value小于min,返回min。 如果value大于max,返回max,否则返回value
        Camera.main.fieldOfView = fov;
        bg.transform.localScale = new Vector3(Mathf.Tan(fov*Mathf.PI/360)*1.6f*3, 1, Mathf.Tan(fov*Mathf.PI/360)*0.9f*3);
        //bg.transform.localScale = new Vector3(Mathf.Tan(fov*Mathf.PI/360)*2.078461f*2.1547f, 1, Mathf.Tan(fov*Mathf.PI/360)*1.125832f*2.1547f)
        // Debug.Log(0.68f/0.5773503f);
        //Debug.Log(1.177794f * 0.629f /0.68f);
    }

    void OnGUI(){
        if(GUI.Button(new Rect(screenWidth - 100,20,btnWidth,btnHeight),"",fangdaStyle)){
            fov += 3* sensitivity*Time.deltaTime;
        }
        if(GUI.Button(new Rect(screenWidth - 180,20,btnWidth,btnHeight),"",suoxiaoStyle)){
            fov -= 3* sensitivity*Time.deltaTime;
        }
    }
}
2014-10-10 06:27:01  
 赞 赞 3

使用道具 登录

269个回答,把该问题分享到群,邀请大神一起回答。
2#
学习了
回复 收起回复
2014-10-10 09:14:19   回复
 赞 赞 3

使用道具 登录

3#
???
回复 收起回复
2018-7-10 14:51:05   回复
 赞 赞 3

使用道具 登录

4#
路过看看 感谢分享
回复 收起回复
2018-10-15 21:18:35   回复
 赞 赞 3

使用道具 登录

5#
哪吒???
回复 收起回复
2018-10-15 22:00:35   回复
 赞 赞 3

使用道具 登录

6#
好教程!感谢楼主分享!
回复 收起回复
2018-10-15 22:07:04   回复
 赞 赞 3

使用道具 登录

7#
好教程!感谢楼主分享!
回复 收起回复
2018-10-15 22:52:54   回复
 赞 赞 3

使用道具 登录

8#
好教程!感谢楼主分享!
回复 收起回复
2018-10-15 22:53:26   回复
 赞 赞 3

使用道具 登录

9#
好教程!感谢楼主分享!
回复 收起回复
2018-10-15 22:53:43   回复
 赞 赞 3

使用道具 登录

10#
好教程!感谢楼主分享!
回复 收起回复
2018-10-15 22:54:09   回复
 赞 赞 3

使用道具 登录

11#
学到了
回复 收起回复
2018-10-15 23:51:34   回复
 赞 赞 3

使用道具 登录

12#
学习了
回复 收起回复
2018-10-16 08:20:09   回复
 赞 赞 3

使用道具 登录

13#
我们先定一个能达到的小目标,先赚它一亿元素币
回复 收起回复
2018-10-16 08:40:31   回复
 赞 赞 3

使用道具 登录

14#
这个牛 牛 牛
回复 收起回复
2018-10-16 08:48:12   回复
 赞 赞 3

使用道具 登录

15#
学习了
回复 收起回复
2018-10-16 08:54:55   回复
 赞 赞 3

使用道具 登录

16#
looking
回复 收起回复
2018-10-16 09:05:03   回复
 赞 赞 3

使用道具 登录

17#
回复 收起回复
2018-10-16 09:07:58   回复
 赞 赞 3

使用道具 登录

18#
asdasfasf
回复 收起回复
2018-10-16 09:08:02   回复
 赞 赞 3

使用道具 登录

19#

学习了
回复 收起回复
2018-10-16 09:09:30   回复
 赞 赞 3

使用道具 登录

20#

感谢楼主
回复 收起回复
2018-10-16 09:11:59   回复
 赞 赞 3

使用道具 登录

CG 游戏行业专业问题

Unity3D技术手机游戏引擎手游引擎
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表