【发布时间】:2015-10-11 01:07:27
【问题描述】:
我试图让用户通过触摸在屏幕上拖动对象并抬起手指以使对象朝它们移动的方向“飞走”。
这就是我正在尝试的。
Vector3 touchPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector3 newPosition = new Vector3 (touchPosition.x, touchPosition.y, transform.position.z);
velocity = newPosition - transform.position;
// User is dragging the object around
if(Input.GetMouseButton(0)) {
transform.position = Vector2.Lerp(transform.position, newPosition, Time.deltaTime * 25f);
}
// User lifts finger
if (Input.GetMouseButtonUp (0)){
transform.GetComponent<Rigidbody2D>().AddForce(velocity, ForceMode2D.Force); // Also tried with transform.GetComponent<Rigidbody2D>().velocity = velocity; and transform.position += velocity; // The later works but doesn't allow for physics later on
}
使用这种方法,对象在拖动时似乎具有低帧速率。此外,当滑动/轻拂对象时,它并不总是保持正确的方向,而且它飞得很慢(我可能必须将它乘以与每秒更新次数相关的因子,但我不确定)。
知道如何使这项工作顺利进行吗?
编辑
我在轻弹对象时通过使用最后几帧的对象速度的平均值来校正方向,但是我仍然无法获得正确的速度。拖动时它的帧率仍然很低(这可以通过使用物理方法而不是直接写入位置来纠正吗?)
【问题讨论】:
-
我在回答中添加了官方来源。