Why this script seems to work only with the first

2019-09-05 22:53发布

问题:

This program is supposed to move my character forward when there is a positive offset in the Y direction of the joystick and stop moveing when the joystick isn't moving. But my code seems to do that only for the first time. The second time onward, it seems to stop moving when I press the button and moves when I release it.

Here is my code:

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);
    }
  }
}

I have a small sphere to which the joystick input is given. And I'm making my character move if there is a positive movement in the sphere's position(offset). So if the sphere moves forward (owing to the joystick input) my character moves. But my character moves continuoslt just after a single movement. Doesn't stop.

回答1:

Nowhere in your code are you pulling user input.

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

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

From here you can apply the values of v and\or h for your movement code.

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);
    }
  }

You can take this a step further if you were wanting to use a 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);

}

an alternative is to apply a force to a rigid body

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

For a more detailed bit of code for handling vertical and horizontal movement from device input I have an open sourced game jam game I did a while back. Also, I have a few code examples