Unity移动操作物体

admin3年前unity基础API787
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...

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

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

Unity3d KeyCode 键盘各种键值详情

 Backspace     退格键  Delete      De...

通过刚体移动的方法

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

Unity场景跳转方法

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

Unity AR 两指手势实现物体放大缩小功能

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

发表评论    

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