unity3D中实现方块能被鼠标拖动的C#脚本

unity3D中实现方块能被鼠标拖动的C#脚本,第1张

这是目前我看到的最精简的,仅一个事件完成拖拽

/

用到了unity3d非常好的协同机制实现这一点,OnMouseDown事件表示鼠标已作了射线判断得到了对象。

拖拽时保持z轴不变,因为屏幕是xy二维的,空间是三维的。

/

IEnumerator OnMouseDown ()

{

var camera = CameramainCamera;

if (camera) {

//转换对象到当前屏幕位置

Vector3 screenPosition = cameraWorldToScreenPoint (transformposition);

//鼠标屏幕坐标

Vector3 mScreenPosition=new Vector3 (InputmousePositionx, InputmousePositiony, screenPositionz);

//获得鼠标和对象之间的偏移量,拖拽时相机应该保持不动

Vector3 offset = transformposition - cameraScreenToWorldPoint( mScreenPosition);

print ("drag starting:"+transformname);

//若鼠标左键一直按着则循环继续

while (InputGetMouseButton (0)) {

//鼠标屏幕上新位置

mScreenPosition = new Vector3 (InputmousePositionx, InputmousePositiony, screenPositionz);

// 对象新坐标

transformposition=offset + cameraScreenToWorldPoint (mScreenPosition);

//协同,等待下一帧继续

yield return new WaitForFixedUpdate ();

}

print ("drag compeleted");

}

}

---------------------------分割线

下面是unity自带的,在Scripts资源包里有拖拽代码,这个拖拽物体必须附加Rigidbody刚体组件。

DragRigidbodyjs

var spring = 500;

var damper = 50;

var drag = 100;

var angularDrag = 50;

var distance = 02;

var attachToCenterOfMass = false;

private var springJoint : SpringJoint;

function Update ()

{

// Make sure the user pressed the mouse down

if (!InputGetMouseButtonDown (0))

return;

var mainCamera = FindCamera();

// We need to actually hit an object

var hit : RaycastHit;

if (!PhysicsRaycast(mainCameraScreenPointToRay(InputmousePosition), hit, 100))

return;

// We need to hit a rigidbody that is not kinematic

if (!hitrigidbody || hitrigidbodyisKinematic)

return;

if (!springJoint)

{

var go = new GameObject("Rigidbody dragger");

var body : Rigidbody = goAddComponent ("Rigidbody") as Rigidbody;

springJoint = goAddComponent ("SpringJoint");

bodyisKinematic = true;

}

springJointtransformposition = hitpoint;

if (attachToCenterOfMass)

{

var anchor = transformTransformDirection(hitrigidbodycenterOfMass) + hitrigidbodytransformposition;

anchor = springJointtransformInverseTransformPoint(anchor);

springJointanchor = anchor;

}

else

{

springJointanchor = Vector3zero;

}

springJointspring = spring;

springJointdamper = damper;

springJointmaxDistance = distance;

springJointconnectedBody = hitrigidbody;

StartCoroutine ("DragObject", hitdistance);

}

function DragObject (distance : float)

{

var oldDrag = springJointconnectedBodydrag;

var oldAngularDrag = springJointconnectedBodyangularDrag;

springJointconnectedBodydrag = drag;

springJointconnectedBodyangularDrag = angularDrag;

var mainCamera = FindCamera();

while (InputGetMouseButton (0))

{

var ray = mainCameraScreenPointToRay (InputmousePosition);

springJointtransformposition = rayGetPoint(distance);

yield;

}

if (springJointconnectedBody)

{

springJointconnectedBodydrag = oldDrag;

springJointconnectedBodyangularDrag = oldAngularDrag;

springJointconnectedBody = null;

}

}

function FindCamera ()

{

if (camera)

return camera;

else

return Cameramain;

}

GameObjectFindGameObjectWithTag(XXXXXX;XXX这些就足够你用的了

GameObjectFind(XXX)GetComponentlt;脚本;()变量 获得并更改

二:

将另一个物体命名为)把你要获取的变量权限定义为public 类型 变量

以下方法任意一个都可以:

一:

public GameObject 另一个物体; //监视面板拖拽赋值

另一个物体GetComponentlt;脚本;();

其他还有很多的方法;()变量GetComponentlt;脚本变量;

三:

将另一个物体命名为

Ray ray = CameramainScreenPointToRay(InputmousePosition);RaycastHit hit;PhysicsRaycast(ray, out hit)用射线判断手指是否触碰物体,碰到就拖动,这里是鼠标的,改成触摸的就好

unity3d用GameObjectFind("游戏对象名")GetComponent<组建名>()具体属性

比如名字叫Player的游戏对象下,有一个Collider组件,我想取得它的大小属性:

var tmpSize = GameObjectFind("Player")GetComponent<Collider>()size;

以上就是关于unity3D中实现方块能被鼠标拖动的C#脚本全部的内容,包括:unity3D中实现方块能被鼠标拖动的C#脚本、unity怎么在一个脚本里获取另一个脚本、unity iphone怎样实现手指触屏拖动物体移动等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址: http://outofmemory.cn/web/9581430.html

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

发表评论

登录后才能评论

评论列表(0条)

保存