Unity移动操作物体

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

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

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

Unity场景跳转方法

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

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

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

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

MoveTowards、Lerp、Slerp

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

Unity 读取Asset目录下物体

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

发表评论    

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