首页 > 解决方案 > 如何通过触摸在屏幕上拖动手指来移动 3D 对象

问题描述

我想通过触摸来移动对象 在屏幕上拖动手指意味着对象应该跟随手指的方向 我在 Play Store 上找到了一个游戏,该游戏球通过触摸它来左右移动,这正是我想要在我的游戏中制作的内容 我的链接游戏:- https://play.google.com/store/apps/details?id=com.ketchapp.hop

标签: c#androidvisual-studiounity3dtouch

解决方案


最简单的做法是将玩家手指在屏幕上的位置设置为一个世界点,并将该世界点设置为 Vector3。将球移动到世界点 Vector3(假设您想使用速度来移动,否则只需使用 .position)。

//Variables
public transform ball;
public float speed = 10f;
//i do not know about touch screen controls. this is something i will link to below!

//Get Touch Position
 Vector3 worldTouchPos = screenTouchPos; //worldTouchPos = the touch position in world space, and the screenTouchPos is the touch position on the screen ( so a value between 0 and 1 in both x and y).

//Ball Movement

ball.position = Vector3.MoveTowards(ball.position //pos to move from\\ ,worldTouchPos //pos to move to\\, speed);

触摸控制链接到 YT 视频:Brackeys 视频/您想观看的其他视频!!!


推荐阅读