2D基本运动UNITY

2D basic movement UNITY

目前我的角色在键盘上工作得很好,但是当我通过 3 个 UI 按钮将你的动作转换为触摸时(我也尝试了 UI 图像,但成功了)我没有成功

基本上是向右,向左,跳跃。

如何使它遵循这些说明:

当用户按下方向键时,角色不会停止行走,直到用户用户松开按钮,而当你按下跳跃玩家跳跃。

这是我用来通过键盘移动的脚本!

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
{

    public float velocity;

    public Transform player;
    private Animator animator;

    public bool isGrounded;
    public float force;

    public float jumpTime = 0.4f;
    public float jumpDelay = 0.4f;
    public bool jumped = false;
    public Transform ground;

    private Gerenciador gerenciador;

    // Use this for initialization
    void Start ()
    {
        gerenciador = FindObjectOfType (typeof(Gerenciador)) as Gerenciador;
        animator = player.GetComponent<Animator> ();
        gerenciador.StartGame ();
    }

    // Update is called once per frame
    void Update ()
    {

        Move ();

    }

    void Move ()
    {

        isGrounded = Physics2D.Linecast (this.transform.position, ground.position, 1 << LayerMask.NameToLayer ("Floor"));
        animator.SetFloat ("run", Mathf.Abs (Input.GetAxis ("Horizontal")));
        if (Input.GetAxisRaw ("Horizontal") > 0) {

            transform.Translate (Vector2.right * velocity * Time.deltaTime);
            transform.eulerAngles = new Vector2 (0, 0);
        }
        if (Input.GetAxisRaw ("Horizontal") < 0) {

            transform.Translate (Vector2.right * velocity * Time.deltaTime);
            transform.eulerAngles = new Vector2 (0, 180);
        }

        if (Input.GetButtonDown ("Vertical") && isGrounded && !jumped) {

            //  rigidbody2D.AddForce (transform.up * force);
            GetComponent<Rigidbody2D> ().AddForce (transform.up * force);
            jumpTime = jumpDelay;
            animator.SetTrigger ("jump");
            jumped = true;
        }

        jumpTime -= Time.deltaTime;

        if (jumpTime <= 0 && isGrounded && jumped) {

            animator.SetTrigger ("ground");
            jumped = false;

        }
    }
}

C# 如果可能,请记住我为这些按钮使用 Canvas UI =]

我认为你应该使用 EventTrigger OnPointerDown 和 OnPointerUp 事件:

http://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html

希望对你有帮助

对于跳转动作,您需要的是一个按钮。 GameObject->UI->Button。将 Image 替换为您自己的图像,然后单击 "Set Native Size"。将按钮的大小重新调整为适合您的游戏的大小。

将按钮附加到下面的跳转按钮插槽脚本。

public Button jumpButton;

void jumpButtonCallBack()
{
    //  rigidbody2D.AddForce (transform.up * force);
    GetComponent<Rigidbody2D>().AddForce(transform.up * force);
    jumpTime = jumpDelay;
    animator.SetTrigger("jump");
    jumped = true;
}


void OnEnable(){
    //Un-Register Button
    jumpButton.onClick.AddListener(() => jumpButtonCallBack());
}

void OnDisable(){
    //Un-register Button
    jumpButton.onClick.RemoveAllListeners();
}

对于移动按钮,您应该使用Button 像跳转按钮一样的组件。您必须实施 Virtual JoyStick 才能使其看起来逼真。 Unity 拥有名为 CrossPlatformInputManager 的资产,可以做到这一点。您必须导入它并稍微修改它才能使用它。观看 this 以了解如何导入它。

现在,您可以将 Input.GetAxisInput.GetAxisRaw 函数替换为 CrossPlatformInputManager.GetAxis("Horizontal")CrossPlatformInputManager.GetAxisRaw("Horizontal")

如果你让它工作,那么可以使用下面的代码使你的代码与移动和桌面兼容。

#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL
//put your Input.GetAxis` and `Input.GetAxisRaw` code here
#elif UNITY_ANDROID || UNITY_IOS 
//Put your `CrossPlatformInputManager.GetAxis("Horizontal")` and `CrossPlatformInputManager.GetAxisRaw("Horizontal")`. here
#endif