为什么这个脚本似乎只能用第一个操纵杆输入?

时间:2016-07-19 16:47:43

标签: c# unity3d game-development

当操纵杆的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);
    }
  }
}

我有一个小球体,操纵杆输入给它。如果球体的位置(偏移)有正向移动,我就会让我的角色移动。 因此,如果球体向前移动(由于操纵杆输入),我的角色会移动。但是我的角色在一次移动之后就会继续移动。不会停止。

1 个答案:

答案 0 :(得分:0)

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

//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