为什么此脚本似乎仅在第一个操纵杆输入上起作用? [英] Why this script seems to work only with the first joystick input?

查看:82
本文介绍了为什么此脚本似乎仅在第一个操纵杆输入上起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序应该在操纵杆的Y方向上存在正偏移时将我的角色向前移动,而在操纵杆不移动时停止移动我的角色.但是我的代码似乎只是第一次这样做.第二次以后,当我按下按钮时似乎停止移动,而当我释放按钮时移动.

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.

这是我的代码:

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.

推荐答案

您在代码中的任何地方都不会拉动用户输入.

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

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

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

如果您想使用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;
}

此示例的详细版本

对于处理来自设备输入的垂直和水平运动的更详细的代码,我有一个代码示例

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

这篇关于为什么此脚本似乎仅在第一个操纵杆输入上起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆