Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在平台游戏中添加触摸按钮?_C#_Android_Unity3d_Touch_Game Engine - Fatal编程技术网

C# 如何在平台游戏中添加触摸按钮?

C# 如何在平台游戏中添加触摸按钮?,c#,android,unity3d,touch,game-engine,C#,Android,Unity3d,Touch,Game Engine,我正在为android开发一款2D视频游戏,我唯一需要完成的就是制作触摸控制。电子游戏只包含左右移动和跳跃(如MarioBros)。我已经创建了画布上的按钮,我只需要脚本,我将非常感谢您的帮助,我附上我的播放器代码,以防您可以帮助我修改 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagem

我正在为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;

    }

}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
使用UnityEngine.SceneManagement;
公共类玩家控制器:单行为
{
公共浮子跳线功率=15f;
私人跳远;
公共场所;
私有刚体2d rb2d;
私人动画师;
私人游戏对象健康栏;
公共游戏对象面板游戏覆盖;
//在第一帧更新之前调用Start
void Start()
{
rb2d=GetComponent();
anim=GetComponent();
healthbar=GameObject.Find(“healthbar”);
}
//每帧调用一次更新
无效更新()
{
if(Input.GetKeyDown(KeyCode.Space)和接地)
{
跳跃=真;
}
动画设置工具(“接地”,接地);
}
void FixedUpdate()
{
{
if(Input.GetKey(KeyCode.RightArrow))
{
if(GetComponent().flipX==true)
{
GetComponent().flipX=false;
}
GetComponent().SetBool(“corr”,true);
transform.Translate(0.1f,0,0);
}
if(Input.GetKey(KeyCode.LeftArrow))
{
if(GetComponent().flipX==false)
{
GetComponent().flipX=true;
}
GetComponent().SetBool(“corr”,true);
transform.Translate(-0.1f,0,0);
}
if(Input.GetKeyUp(KeyCode.LeftArrow)| | Input.GetKeyUp(KeyCode.RightArrow))
{
GetComponent().SetBool(“corr”,false);
}
//萨尔塔
如果(跳转)
{
rb2d.AddForce(矢量2.up*跳线功率,ForceMode2D.脉冲);
跳跃=假;
}
}
//
}
public void EnemyJump()
{
跳跃=真;
}
公共无效EnemyJumpB()
{
healthbar.SendMessage(“TakeDamage”,30);
跳跃=真;
}
}
您可以在UI按钮上附加(或已经附加)按钮组件,您可以这样使用它们:

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!");
    }
}
使用UnityEngine;
使用UnityEngine.UI;
使用系统集合;
公共类点击示例:MonoBehavior{
公共按钮你的按钮;
无效开始(){
Button btn=yourButton.GetComponent();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick(){
Log(“您已经单击了按钮!”);
}
}

Update()和FixedUpdate()脚本中的所有“if”语句都可以替换为适当的“TaskOnClick()”函数(以一种很好的方式命名它们非常感谢,您如何替换“如果”?谢谢,当我单击时它工作,但它不能正常行走,即使我按住键前进,它也会停止,我必须快速按下它,以便它可以前进:(因为它不承认coliders@Jesus从onPointerDown和OnPointerUp函数切换bool,然后在Update()中检查该bool是否为true,并从那里调用OnLeftButton
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)
}