Unity移动操作物体

admin3年前unity基础API626
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);
                }
            }
	}


相关文章

UI组件绑定事件的三种方法

1:在界面中托转事件2:给物体通过代码添加onClick方法n_Start.onClick.AddListener(OnStartButtonClick);//监听点击事件不同的物体事件名字不同 如o...

MoveTowards、Lerp、Slerp

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

Unity寻找子物体的几种方法

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

Unity 手机触摸Touch事件

1:判断是否触摸如果大于0那就是最少一个手指触摸了屏幕 以此类推 如果=2就是2个手指 最多10个if(Input.touchCount >0)2:判断单...

unity 新的输入系统input system 使用

unity 新的输入系统input system 使用

1:首先在包管理器里安装 搜索"input system"2: 创建input acionts3:生成代码  勾选后点击右小角应用就可以自动生成相关代...

发表评论    

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