Unity移动操作物体

admin2年前unity基础API219
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...

相机跟随角色移动镜头缩放方法

这种方法最简单可以实现相机跟随角色平移1:计算相机和目标物体的距离  offset = this.transform.position - ...

MoveTowards、Lerp、Slerp

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

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

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

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

Unity给UI元素物体添加操作响应事件

可以给自定义物体如图片等添加响应事件引入命名空间using UnityEngine.EventSystems;类中继承接口IPointerDownHandler实现接口  ...

通过刚体移动的方法

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

发表评论    

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