我有一个对象,我想通过GetTouch在一定距离抛出。我的代码工作但问题是当我多次触摸屏幕时,对象也移动了多次,我不想要,我想要我的对象无论我触摸或滑动屏幕多少次,都只移动一次。这是我尝试过的东西。
public class RealBallMove : MonoBehaviour {
public float speed;
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.touchCount >0 &&
Input.GetTouch(0).phase == TouchPhase.Ended || (Input.GetMouseButtonDown(0)))
{
//rb.AddForce(Vector3.forward * speed);
//rb.AddForce(Vector3.up * speed);
GetComponent<Rigidbody> ().isKinematic = false;
GetComponent<Rigidbody> ().AddForce (new Vector3(0.0f, 20.0f, 12.0f));
//Destroy (GetComponent<Rigidbody>());
}
} }
答案 0 :(得分:2)
只需添加一个布尔值,表明你是否抛球;)
public class RealBallMove : MonoBehaviour {
public float speed;
public Rigidbody rb;
private bool thrown ;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (
!thrown && (
(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended ) ||
Input.GetMouseButtonDown(0) )
)
{
rb.isKinematic = false;
rb.AddForce (new Vector3(0.0f, 20.0f, 12.0f));
thrown = true ;
}
}
}
另一个选项是在检测到触摸时禁用脚本,但前提是脚本只是上面的行而不是其他内容。