Unity移动操作物体

admin3年前unity基础API456
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 AR 两指手势实现物体放大缩小功能

using UnityEngine;   public class Zoom : MonoBehaviour {  &nb...

unity 新的输入系统input system 使用

unity 新的输入系统input system 使用

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

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

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

Unity 手机触摸Touch事件

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

Unity 插值运算

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

发表评论    

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