Unity移动操作物体

admin3年前unity基础API818
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 手机触摸Touch事件

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

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

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

MoveTowards、Lerp、Slerp

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

Unity场景跳转方法

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

blender导出模型和动画到Unity

blender导出模型和动画到Unity

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

Unity使用input system制作一个摇杆控制物体移动

Unity使用input system制作一个摇杆控制物体移动

1:首先使用UI做个摇杆的模型 两个图片嵌套,外侧是边界,中间图片是摇杆2:使用inputSYSTEM创建事件3:玩家物体上绑定脚本using System.Collections...

发表评论    

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