4个回答
展开全部
这是目前我看到的最精简的,仅一个事件完成拖拽.
/*
* 用到了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;
}
追问
前两天在上学路上,不好意思哈,我们加Q嘛,1554548129
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
继续追问,如果我想把一个物体托放到另一个物体身上,此时删除原来的物体,用这个拖过来的物体替换它,怎么办
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
方法如下:
在鼠标的点击处使用一个射线(ScreenpointToRay),射线垂直投到三维空间里,如果射线接触到你要移动的物体,获得这个对象,鼠标移动时同时改变这个对象的坐标与射线的投射点一致即可,这样的结果就是,鼠标点到物体按住,鼠标到哪里,物体就到哪里。
在鼠标的点击处使用一个射线(ScreenpointToRay),射线垂直投到三维空间里,如果射线接触到你要移动的物体,获得这个对象,鼠标移动时同时改变这个对象的坐标与射线的投射点一致即可,这样的结果就是,鼠标点到物体按住,鼠标到哪里,物体就到哪里。
追问
要的就是这种效果,能否提供段代码,我初学unity3D,想看看几个简单的代码作为入门参考
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询