找回密码
 立即注册
查看: 4725|回复: 97

[基础] Unity3D 第三课如何用滚轮放大缩小你的游戏

[复制链接]
发表于 2013-3-4 22:52 | 显示全部楼层 |阅读模式
资源信息 Tutorial Information
教程名称: Unity3D 第三课 如何用滚轮放大缩小你的游戏(发帖教程)
适用引擎:   (适用引擎,为空默认为Unity)
教程语种: 中文
教程等级: 1
教程格式: 图文(请用IE9以上浏览器访问本版块)
教程作者: 转载自互联网 (如有问题请短消息联系作者或发表回复)
下载地址: (兑换积分)
点击查看原图
美丽分割线
Unity3D 第三课如何用滚轮放大缩小你的游戏
v本课程接上一课Unity3D 第二课如何让摄像机跟随你的角色首先要以你当前控制的角色为中心,
我们需要以下变量:
  • //最小缩小距离
  • public float theDistance = -10f;
  •     //最大缩小距离
  •     public float MaxDistance = -10f;
  •     //缩放速度
  •     public float ScrollKeySpeed = 100.0f;

复制代码

在Update () 初始化
  •       // 滚轮设置 相机与人物的距离.
  •              if(Input.GetAxis("Mouse ScrollWheel") != 0)
  •              {
  •                   theDistance = theDistance + Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * ScrollKeySpeed;
  •              }
  •              // 鼠标滚轮滚动
  •              if(theDistance>0)
  •                   theDistance = 0;
  •              if(theDistance < MaxDistance)
  •                   theDistance = MaxDistance;

复制代码

下面贴出全部代码
  • using UnityEngine;
  • using System.Collections;
  • [AddComponentMenu("Camera-Control/Mouse Look")]
  • public class MouseLook : MonoBehaviour {
  •         public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
  •         public RotationAxes axes = RotationAxes.MouseXAndY;
  •         public float sensitivityX = 15F;
  •         public float sensitivityY = 15F;
  •         public float minimumX = -360F;
  •         public float maximumX = 360F;
  •         public float minimumY = -85F;
  •         public float maximumY = 4F;
  •         public float rotationY = 0F;
  •         public GameObject target;
  •         public float theDistance = -10f;
  •         public float MaxDistance = -10f;
  •         public float ScrollKeySpeed = 100.0f;
  •         void Update ()
  •         {
  •                 target = GameObject.Find("Player");
  •                  // 滚轮设置 相机与人物的距离.
  •              if(Input.GetAxis("Mouse ScrollWheel") != 0)
  •              {
  •                   theDistance = theDistance + Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * ScrollKeySpeed;
  •              }
  •              // 鼠标中间滚动得到的值是不确定的,不会正好就是0,或 -10,当大于0时就设距离为0,小于MaxDistance就设置为MaxDistance
  •              if(theDistance>0)
  •                   theDistance = 0;
  •              if(theDistance < MaxDistance)
  •                   theDistance = MaxDistance;
  •                 if(Input.GetMouseButton(1))
  •                 {
  •                         transform.position = target.transform.position;
  •                         if (axes == RotationAxes.MouseXAndY)
  •                         {
  •                                 float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
  •                                 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  •                                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
  •                                 transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
  •                         }
  •                         else if (axes == RotationAxes.MouseX)
  •                         {
  •                                 transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
  •                         }
  •                         else
  •                         {
  •                                 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  •                                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
  •                                 transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
  •                         }
  •                         SetDistance();
  •                 }
  •                 else
  •             {
  •                 transform.position = target.transform.position;
  •                        SetDistance();
  •             }
  •         }
  •         void Start ()
  •         {
  •                 if (rigidbody)
  •                 {
  •                         rigidbody.freezeRotation = true;
  •                         transform.position = target.transform.position;
  •                 }
  •         }
  •         //设置相机与人物之间的距离
  •         void SetDistance()
  •         {
  •              transform.Translate(Vector3.forward * theDistance);
  •         }
  • }

复制代码

运行游戏看看。现在是不是有点大型游戏的味道了哇……

本帖子中包含更多资源

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

×

评分

参与人数 1鲜花 +1 +1 收起 理由
lukylukymg + 1 + 1 赞一个!

查看全部评分

发表于 2015-6-11 15:45 | 显示全部楼层
马了回去试试~
发表于 2015-6-20 01:17 | 显示全部楼层

感谢楼主的无私分享!{:soso__11402694654016840197_7:}
发表于 2017-3-19 22:06 | 显示全部楼层
楼主是超人
发表于 2017-3-19 21:55 | 显示全部楼层
好帖就是要顶
发表于 2017-3-19 22:14 | 显示全部楼层
真心顶
发表于 2017-3-19 21:58 | 显示全部楼层
说的非常好
发表于 2017-3-19 21:40 | 显示全部楼层
很好哦
发表于 2017-4-12 15:58 | 显示全部楼层
很不错
发表于 2017-4-12 16:10 | 显示全部楼层
好帖就是要顶
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-3 14:59 , Processed in 0.116695 second(s), 32 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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