unity3D中实现方块能被鼠标拖动的C#脚本
在unity3D游戏引擎中我想做一个2D的游戏有一个方块想实现鼠标放在方块上按下左键后不松开并移动鼠标方块能随着鼠标的移动被拖动鼠标若是点击在方块外并移动鼠标的话便没有事...
在unity3D游戏引擎中 我想做一个2D的游戏 有一个方块 想实现鼠标放在方块上按下左键后不松开并移动鼠标 方块能随着鼠标的移动被拖动 鼠标若是点击在方块外并移动鼠标的话便没有事件发生 若用C#脚本该怎么实现 谁能帮我解决了我就给谁分哦
展开
2个回答
展开全部
这是目前我看到的最精简的,仅一个事件完成拖拽.
/*
* 用到了unity3d非常好的协同机制实现这一点,OnMouseDown事件表示鼠标已作了射线判断得到了对象。
* 拖拽时保持z轴不变,因为屏幕是xy二维的,空间是三维的。
* */
IEnumerator OnMouseDown ()
{
var camera = Camera.mainCamera;
if (camera) {
//转换对象到当前屏幕位置
Vector3 screenPosition = camera.WorldToScreenPoint (transform.position);
//鼠标屏幕坐标
Vector3 mScreenPosition=new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);
//获得鼠标和对象之间的偏移量,拖拽时相机应该保持不动
Vector3 offset = transform.position - camera.ScreenToWorldPoint( mScreenPosition);
print ("drag starting:"+transform.name);
//若鼠标左键一直按着则循环继续
while (Input.GetMouseButton (0)) {
//鼠标屏幕上新位置
mScreenPosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);
// 对象新坐标
transform.position=offset + camera.ScreenToWorldPoint (mScreenPosition);
//协同,等待下一帧继续
yield return new WaitForFixedUpdate ();
}
print ("drag compeleted");
}
}
---------------------------分割线
下面是unity自带的,在Scripts资源包里有拖拽代码,这个拖拽物体必须附加Rigidbody刚体组件。
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非常好的协同机制实现这一点,OnMouseDown事件表示鼠标已作了射线判断得到了对象。
* 拖拽时保持z轴不变,因为屏幕是xy二维的,空间是三维的。
* */
IEnumerator OnMouseDown ()
{
var camera = Camera.mainCamera;
if (camera) {
//转换对象到当前屏幕位置
Vector3 screenPosition = camera.WorldToScreenPoint (transform.position);
//鼠标屏幕坐标
Vector3 mScreenPosition=new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);
//获得鼠标和对象之间的偏移量,拖拽时相机应该保持不动
Vector3 offset = transform.position - camera.ScreenToWorldPoint( mScreenPosition);
print ("drag starting:"+transform.name);
//若鼠标左键一直按着则循环继续
while (Input.GetMouseButton (0)) {
//鼠标屏幕上新位置
mScreenPosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);
// 对象新坐标
transform.position=offset + camera.ScreenToWorldPoint (mScreenPosition);
//协同,等待下一帧继续
yield return new WaitForFixedUpdate ();
}
print ("drag compeleted");
}
}
---------------------------分割线
下面是unity自带的,在Scripts资源包里有拖拽代码,这个拖拽物体必须附加Rigidbody刚体组件。
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;
}
展开全部
呃..以前做过..代码找不到了..
跟你说下思路吧..
鼠标拖动的操作是由三个时间组成的..
鼠标按住 鼠标移动 鼠标松开.
定义一个公用变量.
鼠标按住的时候,..变量为true
松开的时候..变量为false
而鼠标移动的时候,,必须为true才可以使对象移动..
移动时先计算鼠标到父窗口的相对位置,和对象的相对位置...然后得到相差值..之后鼠标移动的时候获取到的鼠标位置加上或者减去这个相对值...就是对象应该在的位置..
跟你说下思路吧..
鼠标拖动的操作是由三个时间组成的..
鼠标按住 鼠标移动 鼠标松开.
定义一个公用变量.
鼠标按住的时候,..变量为true
松开的时候..变量为false
而鼠标移动的时候,,必须为true才可以使对象移动..
移动时先计算鼠标到父窗口的相对位置,和对象的相对位置...然后得到相差值..之后鼠标移动的时候获取到的鼠标位置加上或者减去这个相对值...就是对象应该在的位置..
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询