仅在上升时在轴上进行平滑的触摸运动?
Smooth touch movement on axis only while going up?
我是 Unity 和 c# 的新手。我正在为手机创建一个项目。我只想在 Jet 上升时通过左右触摸来移动 Jet Axis 位置
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
//side to side movement
if (touch.position.x < Screen.width / 2)
rb.velocity = new Vector2(- 2f, transform.position.y);
if (touch.position.x > Screen.width / 2)
rb.velocity = new Vector2(+ 2f, transform.position.y);
}
break;
case TouchPhase.Ended:
rb.velocity = new Vector2(0f, 0f);
break;
}
喷气机有 Addforce,所以每当我左右触碰时,喷气机都会减速。
喷气代码:
switch (JetOn)
{
case true:
StartCoroutine(BurnFuel());
rb.AddForce(new Vector2(0f, JumpForce), ForceMode2D.Force);
break;
case false:
rb.AddForce(new Vector2(0f, 0f), ForceMode2D.Force);
break;
}
您不应混合使用 AddForce
和手动分配给 velocity
。直接分配给 velocity
会使 AddForce
行为不可预测,并且通常会导致它永远无法工作。
选择其中之一,并将其用于制作该帧所需的每个速度变化。
这是您的代码示例,仅分配给 velocity
:
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
//side to side movement
if (touch.position.x < Screen.width / 2)
// I think you mean rb.velocity.y here instead of transform.position.y
rb.velocity = new Vector2(- 2f, rb.velocity.y);
if (touch.position.x > Screen.width / 2)
rb.velocity = new Vector2(+ 2f, rb.velocity.y);
}
break;
case TouchPhase.Ended:
rb.velocity = new Vector2(0f, 0f);
break;
}
}
...
switch (JetOn)
{
case true:
StartCoroutine(BurnFuel());
rb.velocity += new Vector2(0f, JumpForce) / rb.mass;
break;
case false:
// unnecessary but included for example purposes
// rb.velocity += new Vector2(0f, 0f) / rb.mass;
break;
}
我是 Unity 和 c# 的新手。我正在为手机创建一个项目。我只想在 Jet 上升时通过左右触摸来移动 Jet Axis 位置
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
//side to side movement
if (touch.position.x < Screen.width / 2)
rb.velocity = new Vector2(- 2f, transform.position.y);
if (touch.position.x > Screen.width / 2)
rb.velocity = new Vector2(+ 2f, transform.position.y);
}
break;
case TouchPhase.Ended:
rb.velocity = new Vector2(0f, 0f);
break;
}
喷气机有 Addforce,所以每当我左右触碰时,喷气机都会减速。
喷气代码:
switch (JetOn)
{
case true:
StartCoroutine(BurnFuel());
rb.AddForce(new Vector2(0f, JumpForce), ForceMode2D.Force);
break;
case false:
rb.AddForce(new Vector2(0f, 0f), ForceMode2D.Force);
break;
}
您不应混合使用 AddForce
和手动分配给 velocity
。直接分配给 velocity
会使 AddForce
行为不可预测,并且通常会导致它永远无法工作。
选择其中之一,并将其用于制作该帧所需的每个速度变化。
这是您的代码示例,仅分配给 velocity
:
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
//side to side movement
if (touch.position.x < Screen.width / 2)
// I think you mean rb.velocity.y here instead of transform.position.y
rb.velocity = new Vector2(- 2f, rb.velocity.y);
if (touch.position.x > Screen.width / 2)
rb.velocity = new Vector2(+ 2f, rb.velocity.y);
}
break;
case TouchPhase.Ended:
rb.velocity = new Vector2(0f, 0f);
break;
}
}
...
switch (JetOn)
{
case true:
StartCoroutine(BurnFuel());
rb.velocity += new Vector2(0f, JumpForce) / rb.mass;
break;
case false:
// unnecessary but included for example purposes
// rb.velocity += new Vector2(0f, 0f) / rb.mass;
break;
}