【问题标题】:Why this script seems to work only with the first joystick input?为什么这个脚本似乎只适用于第一个操纵杆输入?
【发布时间】:2016-07-19 16:47:42
【问题描述】:

这个程序应该在操纵杆的 Y 方向有正偏移时向前移动我的角色,并在操纵杆不移动时停止移动。但我的代码似乎只是第一次这样做。第二次以后,当我按下按钮时它似乎停止移动,当我松开它时它会移动。

这是我的代码:

using UnityEngine;
using System.Collections;

public class move : MonoBehaviour {
  public Transform obj;
  float threshold = 0.0f;   
  Vector3 current_pos;

  void Start () {
    current_pos = obj.position;
  } 

  public void Update() {
    Vector3 offset = obj.position - current_pos; 
    if(offset.y > threshold) { // goes forward
      transform.Translate(Vector3.forward *10* Time.deltaTime);
      transform.Translate(Vector3.up * Time.deltaTime, Space.World);
    }

    if (offset.y < threshold){ // goes reverse
      transform.Translate(-Vector3.forward *10* Time.deltaTime);
      transform.Translate(Vector3.up *10* Time.deltaTime, Space.World);
    }
  }
}

我有一个小球体,可以输入操纵杆。如果球体的位置(偏移)有正向移动,我会让我的角色移动。 因此,如果球体向前移动(由于操纵杆输入),我的角色就​​会移动。但是我的角色在一个动作之后就连续移动。停不下来。

【问题讨论】:

  • 你听说过调试吗?它是关于逐步运行您的应用程序,您可以检查所有值是否符合您的期望。
  • 为什么不在更新函数中更新当前位置?您的 start 函数中的 if 语句应该 - 据我了解 - 始终返回 false,因为当前位置是对象位置,两者之间的差异应该为零。
  • 另外,任何时间为零的东西总是为零——尽管我假设你知道这一点。我这么说是因为transform.Translate(Vector3.up *0* Time.deltaTime, Space.World); 和它上面的行都乘以零。可能是错字?还是故意的?因为你的转变为零。
  • 我想如果我更新当前位置,那么就不会有偏移量,是吗?因为说操纵杆的正常位置是(0,0,0),而 obj.position 是不断变化的。至少这是我在程序中想要的。 ://
  • 不清楚你想用这段代码实现什么。通过详细说明您最终想要做什么来更新您的问题

标签: c# unity3d game-development


【解决方案1】:

您在代码中的任何地方都没有提取用户输入。

//getting the x axis from a controller
var v = Input.GetAxis("Vertical");

//getting the y axis from a controller
var h = Input.GetAxis("Horizontal");

您可以在此处为您的移动代码应用 v 和\或 h 的值。

public void Update() {
    var h = Input.GetAxis("Horizontal");
    Vector3 offset = obj.position - current_pos; 
    if(h > threshold) { // goes forward
      transform.Translate(Vector3.forward *10* Time.deltaTime);
      transform.Translate(Vector3.up * Time.deltaTime, Space.World);
    }

    if (h < threshold){ // goes reverse
      transform.Translate(-Vector3.forward *10* Time.deltaTime);
      transform.Translate(Vector3.up *10* Time.deltaTime, Space.World);
    }
  }

如果您想使用rigidbody2d,可以更进一步

private void CheckInput()
{

  //getting the y axis from a controller
  var h = Input.GetAxis("Horizontal");
  var moveVector = new Vector2(h*Speed, 0);
  Vector3 offset = obj.position - current_pos; 
  PlayerRigidbody2D.velocity = new Vector2(moveVector.x, moveVector.y);

}

另一种方法是对刚体施加力

public bool facingRight = true;
public float moveForce = 365f;          // Amount of force added to move the player left and right.
public float maxSpeed = 5f;             // The fastest the player can travel in the x axis.


void FixedUpdate ()
{
    // Cache the horizontal input.
    float h = Input.GetAxis ("Horizontal");

    // The Speed animator parameter is set to the absolute value of the horizontal input.
    anim.SetFloat ("Speed", Mathf.Abs (h));

    // If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...
    if (h * rigidbody2D.velocity.x < maxSpeed)
    {
        // ... add a force to the player.
        rigidbody2D.AddForce (Vector2.right * h * moveForce);
    }

    // If the player's horizontal velocity is greater than the maxSpeed...
    if (Mathf.Abs (rigidbody2D.velocity.x) > maxSpeed)
    {
        // ... set the player's velocity to the maxSpeed in the x axis.
        rigidbody2D.velocity = new Vector2 (Mathf.Sign (rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
    }

    // If the input is moving the player right and the player is facing left...
    if (h > 0 && !facingRight)
    {
        // ... flip the player.
        Flip ();
    }
    else if (h < 0 && facingRight) // Otherwise if the input is moving the player left and the player is facing right...
    {
        // ... flip the player.
        Flip ();
    }
}


void Flip ()
{
    //Switch the way the player is labelled as facing.
    facingRight = !facingRight;

    // Multiply the player's x local scale by -1.
    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;
}

Detailed version of this Example

有关处理来自设备输入的垂直和水平移动的更详细的代码,我有一个open sourced game jam game 我不久前做过。另外,我有几个code examples

【讨论】:

  • 您好,我也尝试过使用您的代码。我的实际问题是游戏对象即使在没有从操纵杆输入的情况下也会移动。第一次打开应用程序时,它会等待正输入,然后继续移动。
  • 你能告诉我你更新后的代码是什么样的吗?
  • 谢谢 Terrance。我想它可以很好地配合刚体运动:)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
  • 2015-05-25
  • 2014-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多