纳金网

标题: u3d04鼠标拖拽物体(转) [打印本页]

作者: 她。    时间: 2012-12-29 10:47
标题: u3d04鼠标拖拽物体(转)
代码清单如下:(未验证)
//將代碼附加到要被拖拽的collider物體上
function Update () {
}
var screenSpace;
var offset;
function OnMouseDown(){
//translate the cubes position from the world to Screen Point
//轉換對象位置,從世界點到屏幕座標
screenSpace = Camera.main.WorldToScreenPoint(transform.position);
//calculate any difference between the cubes world position and the mouses Screen position converted to a worldpoint
//在對象世界座標與鼠標屏幕座標計算任何不同,轉換到世界點上
offset = transform.position – Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, screenSpace.z));
}
/*
OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.OnMouseDrag is called every frame while the mouse is down.
*/
function OnMouseDrag () {
//keep track of the mouse position
//保持鼠標位置追蹤
var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
//convert the screen mouse position to world point and adjust with offset
//轉換屏幕鼠標位置到世界點,以及通過偏移調整
var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
//update the position of the object in the world
//更新物體在世界的位置
transform.position = curPosition;
}


作者: 她。    时间: 2012-12-29 10:54
如果觉得好的话就赞一个吧~
作者: 狂风大尉    时间: 2012-12-31 02:43
谢谢楼主的帖子分享,学习了





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5