Unity移动操作物体

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


相关文章

MoveTowards、Lerp、Slerp

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

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

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

Unity 读取Asset目录下物体

读取方法:AssetDatabase.LoadAssetAtPath使用方法path = "Assets/CustomizableAnimeGirl/Models/Mat...

Unity 插值运算

在unity3d中经常用线性插值函数Lerp()来在两者之间插值,两者之间可以是两个材质之间、两个向量之间、两个浮点数之间、两个颜色之间,其函数原型如下: 1.Material.Lerp&nbs...

通过刚体移动的方法

1:MovePosion移动的新的位置,传入的参数是最新的位置 一般是旧的位置加上变化的参数2:velocity这个方法是瞬间给物体一个恒定的速度,将物体提升至该速度 3:addF...

unity 新的输入系统input system 使用

unity 新的输入系统input system 使用

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

发表评论    

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