unity3d中判断鼠标是不是在屏幕指定区域内
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity3d中判断鼠标是不是在屏幕指定区域内相关的知识,希望对你有一定的参考价值。
如图,游戏界面内有3个camera,处于屏幕中不同范围
用鼠标可以控制camera视角,如滚轮缩放,右键旋转等,但要求只有在屏幕中该camera范围内操作鼠标才可以控制,并且一次只能控制单个camera
现在我有控制的脚本,但无论在屏幕哪个位置操作鼠标,三个镜头都会跟着一起动。
DragRigidbody.js
var spring = 50.0;
var damper = 5.0;
var drag = 10.0;
var angularDrag = 5.0;
var distance = 0.2;
var attachToCenterOfMass = false;
private var springJoint : SpringJoint;
function Update ()
// Make sure the user pressed the mouse down
if (!Input.GetMouseButtonDown (0))
return;
var mainCamera = FindCamera();
// We need to actually hit an object
var hit : RaycastHit;
if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit, 100))
return;
// We need to hit a rigidbody that is not kinematic
if (!hit.rigidbody || hit.rigidbody.isKinematic)
return;
if (!springJoint)
var go = new GameObject("Rigidbody dragger");
var body : Rigidbody = go.AddComponent ("Rigidbody") as Rigidbody;
springJoint = go.AddComponent ("SpringJoint");
body.isKinematic = true;
springJoint.transform.position = hit.point;
if (attachToCenterOfMass)
var anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
anchor = springJoint.transform.InverseTransformPoint(anchor);
springJoint.anchor = anchor;
else
springJoint.anchor = Vector3.zero;
springJoint.spring = spring;
springJoint.damper = damper;
springJoint.maxDistance = distance;
springJoint.connectedBody = hit.rigidbody;
StartCoroutine ("DragObject", hit.distance);
function DragObject (distance : float)
var oldDrag = springJoint.connectedBody.drag;
var oldAngularDrag = springJoint.connectedBody.angularDrag;
springJoint.connectedBody.drag = drag;
springJoint.connectedBody.angularDrag = angularDrag;
var mainCamera = FindCamera();
while (Input.GetMouseButton (0))
var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
springJoint.transform.position = ray.GetPoint(distance);
yield;
if (springJoint.connectedBody)
springJoint.connectedBody.drag = oldDrag;
springJoint.connectedBody.angularDrag = oldAngularDrag;
springJoint.connectedBody = null;
function FindCamera ()
if (camera)
return camera;
else
return Camera.main;
编程回忆录已经有八年的历史,是教程团队旗下的一个在线网络培训机构,目前已录制十三个科目,涉及到互联网开发,软件开发,游戏开发(虚拟现实开发),现在编程回忆录的Unity3D是国内唯一一家中文连载的大型视频教程。零基础开始讲解,通俗易懂 ,以实战为目地,受到众多网友的一致好评。
以上是关于unity3d中判断鼠标是不是在屏幕指定区域内的主要内容,如果未能解决你的问题,请参考以下文章