Unity移动操作物体

admin3年前unity基础API628
if (Input.touchCount == 1)
        {
            Touch firstTouch = Input.GetTouch(0);
            //判断是否当前物体
            if (firstTouch.phase == TouchPhase.Began)
            {
                Ray ray = Camera.main.ScreenPointToRay(firstTouch.position);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit))
                {
                		//获取当前触摸到的物体
                        currTouchObj = hit.collider.transform;
                }
            }

            if (Input.GetTouch(0).phase == TouchPhase.Moved)
            {     
              //如果移动触摸了当前物体
                if (currTouchObj)
                {
                 //控制移动
                    Vector3 touchDeltaPos = Input.GetTouch(0).deltaPosition;
                    currTouchObj.Translate(touchDeltaPos.x * touchObjMoveSpeed, touchDeltaPos.y * touchObjMoveSpeed, 0, Space.World);
                }
            }
	}


相关文章

Unity3d KeyCode 键盘各种键值详情

 Backspace     退格键  Delete      De...

MoveTowards、Lerp、Slerp

1:MoveTowards MoveTowards(Vector2 current, Vector2 target, float maxDistanceDelta);current:当前位置...

blender导出模型和动画到Unity

blender导出模型和动画到Unity

1:首先我们确保导出的模型是FBX2:模型的Rig设置为泛型 avatar根据实际情况可以选择无或则从此模型创建3:设置动画控制器,把模型中的动画拖入就可以了...

Unity寻找子物体的几种方法

    1: transform.Find()寻找子物体   transform.Find(“子物体名字”)2: GameObject.FindGam...

利用射线获取被点击的物体信息

首先被点击的物体需要加上碰撞体Collider监听点击代码:鼠标点击:if (Input.GetMouseButtonDown(0))     &n...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。