c# - 对象跟随手指仅当它在屏幕上滑动时
问题描述
我有一个球体正在向前移动,当它在屏幕上滑动时我需要它平稳地跟随我的手指,但只能在 x 轴上。我试图做出确切的动作,比如Rolling Sky,Color Road
当球在 x 轴上滑动并平稳移动时,它应该跟随我的手指。
就像我之前提到的游戏一样。我尝试OnMouseDrag
了很多方法,但它不能正常工作,因为它不跟随手指,或者在手指滑动时它不移动。
解决方案
据我了解,您似乎想获取用户手指的位置并相应地移动球?
例子:
private void Update()
{
Touch touch = Input.GetTouch(0); // Get the touch data for the first finger
Vector2 position = touch.position; // Get the position in screen-space
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(position); // Convert the position to world space
Vector3 ballPosition = ball.transform.position;
ball.transform.position = new Vector3(worldPosition.x, ballPosition.y, ballPosition.z); // Move the ball
}
推荐阅读
- c++ - 如何获取卡序列号?
- firebase - Flutter 实现自定义弹窗广告
- android-recyclerview - 回收视图相同的数据加载两次
- rest - 如何创建读取/查看文件的权限?
- ethereum - 以“SupportingFeeOnTransferTokens”结尾的 Uniswap Router 合约函数如何工作?
- html - 如何在不模糊此网页中的文本和导航栏的情况下模糊背景图像?
- java - 使用 java-algs4 编译和运行一个简单的 java 程序
- java - 在 Controller 对象的 initialize() 方法期间无法访问父节点,但稍后访问时可以
- java - 如何在关闭连接之前等待 RabbitMQ 获取消费数据
- android-gradle-plugin - Apk反编译:自动匹配外部库