【问题标题】:How to add touch buttons in a platform game?如何在平台游戏中添加触摸按钮?
【发布时间】:2020-01-17 18:13:13
【问题描述】:

我正在为 android 开发 2D 视频游戏,我唯一需要完成的就是制作触摸控件。视频游戏仅包含左右移动和跳跃(如 MarioBros)。我已经在画布上创建了按钮,我只需要脚本,非常感谢您的帮助,我附上我的播放器代码,以防您帮助我进行修改。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;


public class PlayerController : MonoBehaviour
{
    public float jumpPower = 15f;
    private bool jump;
    public bool grounded;
    private Rigidbody2D rb2d;
    private Animator anim;
    private GameObject healthbar;
    public GameObject panelGameOver;



    // Start is called before the first frame update
    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();

        healthbar = GameObject.Find("Healthbar");
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && grounded)
        {
            jump = true;
        }
        anim.SetBool("Grounded", grounded);
    }

    void FixedUpdate()
    {

        {
            if (Input.GetKey(KeyCode.RightArrow))
            {
                if (GetComponent<SpriteRenderer>().flipX == true)
                {
                    GetComponent<SpriteRenderer>().flipX = false;
                }
                GetComponent<Animator>().SetBool("Correr", true);
                transform.Translate(0.1f, 0, 0);
            }
            if (Input.GetKey(KeyCode.LeftArrow))
            {
                if (GetComponent<SpriteRenderer>().flipX == false)
                {
                    GetComponent<SpriteRenderer>().flipX = true;
                }
                GetComponent<Animator>().SetBool("Correr", true);
                transform.Translate(-0.1f, 0, 0);
            }
            if (Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow))
            {
                GetComponent<Animator>().SetBool("Correr", false);
            }
            //Saltar
            if (jump)
            {
                rb2d.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
                jump = false;
            }

        }






        //
    }
    public void EnemyJump()
    {
        jump = true;


    }

    public void EnemyJumpB()
    {
        healthbar.SendMessage("TakeDamage", 30);
        jump = true;

    }

}

【问题讨论】:

    标签: c# android unity3d touch game-engine


    【解决方案1】:

    你可以在你的 UI 按钮上附加(或者你已经附加了一个)Button 组件,你可以像这样使用它们:

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    
    public class ClickExample : MonoBehaviour {
        public Button yourButton;
    
        void Start () {
            Button btn = yourButton.GetComponent<Button>();
            btn.onClick.AddListener(TaskOnClick);
        }
    
        void TaskOnClick(){
            Debug.Log ("You have clicked the button!");
        }
    }
    

    您在 Update() 和 FixedUpdate() 脚本中的所有“if”语句都可以替换为适当的“TaskOnClick()”函数(以一种很好的方式命名它们

    “如果”的:

    public Button leftButton;
    
    private void OnLeftButton() {
        if (GetComponent<SpriteRenderer>().flipX == false) {
            GetComponent<SpriteRenderer>().flipX = true;
        }
        GetComponent<Animator>().SetBool("Correr", true);
        transform.Translate(-0.1f, 0, 0);
    }
    
    private void Awake() {
        this.leftButton.onClick.AddListener(OnLeftButton)
    }
    

    【讨论】:

    • 非常感谢,如何替换'if'?
    • 谢谢,按一下就可以了,但是不能正常走,即使长按键前进它也停了,我要快按才能前进: (因为它不识别 coliders
    • @Jesus 从 onPointerDown 和 OnPointerUp 函数中切换一个 bool,然后在 Update() 中检查该 bool 是否为真,并从那里调用 OnLeftButton
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多