为什么我的 TouchPhase.Began 不能这样工作?
Why does my TouchPhase.Began not work like this?
我不明白为什么 Input.GetTouch
在这里不起作用。
private void Update()
{
Vector2 vel = rb.velocity;
float ang = Mathf.Atan2(vel.y, x: 10) * Mathf.Rad2Deg;
if (Input.GetKey(KeyCode.Space))
{
rb.AddForce(Vector2.up * gravity * Time.deltaTime * 2000f);
}
if (Input.GetTouch(TouchPhase.Began))
{
rb.AddForce(Vector2.up * gravity * Time.deltaTime * 2000f);
}
}
Input.GetTouch
需要一个索引.. 您正在传递一个枚举值。
API 实际上有几个如何在 Unity 中使用触摸的示例。
在您的情况下,您只想检查状态 Began
中是否有第一次触摸,因此您可以使用例如
private void Update () {
Vector2 vel = rb.velocity;
float ang = Mathf.Atan2 (vel.y, x : 10) * Mathf.Rad2Deg;
if (Input.GetKey (KeyCode.Space)) {
rb.AddForce (Vector2.up * gravity * Time.deltaTime * 2000f);
}
if(Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
rb.AddForce (Vector2.up * gravity * Time.deltaTime * 2000f);
}
}
}
我不明白为什么 Input.GetTouch
在这里不起作用。
private void Update()
{
Vector2 vel = rb.velocity;
float ang = Mathf.Atan2(vel.y, x: 10) * Mathf.Rad2Deg;
if (Input.GetKey(KeyCode.Space))
{
rb.AddForce(Vector2.up * gravity * Time.deltaTime * 2000f);
}
if (Input.GetTouch(TouchPhase.Began))
{
rb.AddForce(Vector2.up * gravity * Time.deltaTime * 2000f);
}
}
Input.GetTouch
需要一个索引.. 您正在传递一个枚举值。
API 实际上有几个如何在 Unity 中使用触摸的示例。
在您的情况下,您只想检查状态 Began
中是否有第一次触摸,因此您可以使用例如
private void Update () {
Vector2 vel = rb.velocity;
float ang = Mathf.Atan2 (vel.y, x : 10) * Mathf.Rad2Deg;
if (Input.GetKey (KeyCode.Space)) {
rb.AddForce (Vector2.up * gravity * Time.deltaTime * 2000f);
}
if(Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
rb.AddForce (Vector2.up * gravity * Time.deltaTime * 2000f);
}
}
}