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

Unity3D 第三课如何用滚轮放大缩小你的游戏,第1张

p=1433首先要以你当前控制的角色为中心,我们需要以下变量: //最小缩小距离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 = 0if(theDistance <MaxDistance)theDistance = MaxDistance下面贴出全部代码using UnityEngineusing System.Collections[AddComponentMenu("Camera-Control/Mouse Look")]public class MouseLook : MonoBehaviour { public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }public RotationAxes axes = RotationAxes.MouseXAndYpublic float sensitivityX = 15Fpublic float sensitivityY = 15Fpublic float minimumX = -360Fpublic float maximumX = 360Fpublic float minimumY = -85Fpublic float maximumY = 4Fpublic float rotationY = 0Fpublic GameObject targetpublic float theDistance = -10fpublic float MaxDistance = -10fpublic float ScrollKeySpeed = 100.0fvoid 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就设置为MaxDistanceif(theDistance>0)theDistance = 0if(theDistance <MaxDistance)theDistance = MaxDistanceif(Input.GetMouseButton(1)){transform.position = target.transform.positionif (axes == RotationAxes.MouseXAndY){float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityXrotationY += Input.GetAxis("Mouse Y") * sensitivityYrotationY = 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") * sensitivityYrotationY = Mathf.Clamp (rotationY, minimumY, maximumY)transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0)}SetDistance()}else{transform.position = target.transform.positionSetDistance()}} void Start (){if (rigidbody){rigidbody.freezeRotation = truetransform.position = target.transform.position}} //设置相机与人物之间的距离void SetDistance(){transform.Translate(Vector3.forward * theDistance)}} 运行游戏看看。

第一种:\x0d\x0a这是一种发射线,获取当前点击的物体(具有碰撞器)的坐标点(也就是碰撞器范围的坐标点)\x0d\x0aif (Input.GetMouseButtonDown(0))\x0d\x0a{\x0d\x0aRaycastHit hitt = new RaycastHit()\x0d\x0aRay ray =Camera.main.ScreenPointToRay(Input.mousePosition)\x0d\x0aPhysics.Raycast(ray, out hitt)\x0d\x0aDebug.Log(hitt.point)\x0d\x0a//Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition))\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a第二种:\x0d\x0aCamera TempC//定义摄像机变量\x0d\x0aprivate void Start()\x0d\x0a{\x0d\x0aTempC = this.GetComponent()//获取摄像机的组件 Camera\x0d\x0a}\x0d\x0avoid Update()\x0d\x0a{\x0d\x0aif(Input.GetMouseButtonDown(0))\x0d\x0a{ \x0d\x0aDebug.Log(TempC.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, 10.0f)))\x0d\x0a}\x0d\x0a}\x0d\x0a注意:这里容易出现的问题是,从鼠标坐标点转成世界坐标点的时候,新的三维向量Z轴的赋值必须不能是0.0f,因为,屏幕坐 标点的Z轴,其实是相对于当前摄像机的,如果是0,只能转一次世界坐标系,剩下的不管点哪里都是一个值,所以不能把Z值设为和摄像机的Z坐标重合,我们需要设置一个非0的数字,最后转成世界坐标系后的值是,摄像机的Z轴加上所设置的值。例如当前摄像机Z坐标是10f,我所设置的Z值是10f,最后转成世界坐标系后的Z值就是20;下面是老外的经典回复。\x0d\x0aThereare two problems here. The first one is that you need a 'new' in front of yourVector3() in C#. The second is that the 'z' parameter must be the distance infront of the camera. In perspective mode at least, passing 0 will causeScreenToWorldPoint() to always return the position of the camera. So the callwill look something like:\x0d\x0a\x0d\x0aworldPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, 10.0f))\x0d\x0aNotethe measurement for the 'z' parameter is from camera plane to the playingsurface plane. It is not the distance between the camera and the object. If thecamera is aligned with the axes, then the distance is easy to calculate. If thecamera is at an angle (i.e. not axes aligned), then ScreenToWorldPoint() is notthe way to go.


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11306017.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存