【问题标题】:2D basic movement UNITY2D基本运动UNITY
【发布时间】:2016-04-25 07:42:10
【问题描述】:

目前我的角色在键盘上完美运行,但是当我通过 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 =]

【问题讨论】:

  • 为什么不使用触摸输入?在这里您可以找到更多详细信息:Input.touches.
  • 从 UnityEngine.EventSystems 实现 IPointerDownHandler 和 IPointerUpHandler (听起来比它还糟糕,事件系统的教程很多)
  • 如果您在必要时没有在问题中添加代码,您将被否决。很高兴你改变了这一点。

标签: c# android unity3d unityscript


【解决方案1】:

对于跳转动作,您需要一个按钮。 GameObject->UI->按钮。将图像替换为您自己的图像,然后单击“设置本机大小”。将按钮重新调整为适合您的游戏的大小。

将 Button 附加到下面的 Jump Button 插槽脚本。

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.GetAxis 和 Input.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

【讨论】:

  • 又一次你给了我最好的soo far asnwer!再次 Ty =]
【解决方案2】:

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

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

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    相关资源
    最近更新 更多