Unity移动操作物体

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


相关文章

Unity场景跳转方法

实现跳转不到的场景,需要先把场景添加到Build中首先引入命名空间using UnityEngine.SceneManagement;然后使用方法跳转 SceneManager.L...

相机跟随角色移动旋转方法

相机跟随角色移动旋转方法

1:首先做个一个空的节点作为相机的根节点 参数归02:在根节点添加一个节点作为缩放 镜头上下移动 Z 镜头远近 Y 镜头上下3:最后添加真正的镜头对象Camer...

MoveTowards、Lerp、Slerp

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

Unity 读取Asset目录下物体

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

通过刚体移动的方法

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

发表评论    

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