unity3D中实现方块能被鼠标拖动的C#脚本
发布网友
发布时间:2022-04-09 13:01
我来回答
共2个回答
热心网友
时间:2022-04-09 14:30
这是目前我看到的最精简的,仅一个事件完成拖拽.
/*
* 用到了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;
}
热心网友
时间:2022-04-09 15:48
呃..以前做过..代码找不到了..
跟你说下思路吧..
鼠标拖动的操作是由三个时间组成的..
鼠标按住 鼠标移动 鼠标松开.
定义一个公用变量.
鼠标按住的时候,..变量为true
松开的时候..变量为false
而鼠标移动的时候,,必须为true才可以使对象移动..
移动时先计算鼠标到父窗口的相对位置,和对象的相对位置...然后得到相差值..之后鼠标移动的时候获取到的鼠标位置加上或者减去这个相对值...就是对象应该在的位置..