C# 用于使角色左右移动的Unity UI按钮;行不通

C# 用于使角色左右移动的Unity UI按钮;行不通,c#,unity3d,C#,Unity3d,我正在为手机制作一个2D游戏,我想使用UI按钮作为控制器,而不是在笔记本电脑上测试游戏时使用的键盘键 以下是我用于角色的代码: public float moveSpeed; private float moveVelocity; public float jumpHeight; public Transform tagGround; public float groundCheckRadius; public LayerMask playerMask; private bool IsGroun

我正在为手机制作一个2D游戏,我想使用UI按钮作为控制器,而不是在笔记本电脑上测试游戏时使用的键盘键

以下是我用于角色的代码:

public float moveSpeed;
private float moveVelocity;
public float jumpHeight;
public Transform tagGround;
public float groundCheckRadius;
public LayerMask playerMask;
private bool IsGrounded;
public Animator anim;

void Start ()
{
    anim = GetComponent<Animator>();
}

void FixedUpdate ()
{
    IsGrounded = Physics2D.OverlapCircle (tagGround.position, groundCheckRadius, playerMask);
}


void Update()
{
    moveVelocity = 0f;

    if (Input.GetKeyDown (KeyCode.W) && IsGrounded) {
        Jump ();
    }

    if (Input.GetKey (KeyCode.D)) {
        Right ();
    }

    if (Input.GetKey (KeyCode.A)) {
        Left ();
    }

    GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveVelocity, GetComponent<Rigidbody2D> ().velocity.y);

    anim.SetFloat ("Speed", Mathf.Abs(GetComponent<Rigidbody2D> ().velocity.x)); 
    anim.SetBool ("isGrounded", IsGrounded);
    anim.SetBool ("Attacking", false);

    if (Input.GetKeyDown (KeyCode.S)) {
        Atk ();
    }

    if (GetComponent<Rigidbody2D> ().velocity.x > 0)
        transform.localScale = new Vector3(1f, 1f, 1f);
    else if (GetComponent<Rigidbody2D> ().velocity.x < 0)
        transform.localScale = new Vector3(-1f, 1f, 1f);

}
公共浮动速度;
私有浮动速度;
公众浮标高度;
公共场所;
公众浮标地面检查半径;
公共层任务玩家任务;
私人学校停课;
公共动画;
无效开始()
{
anim=GetComponent();
}
无效固定更新()
{
IsGround=Physics2D.Overlap圆(标记ground.position、groundCheckRadius、playerMask);
}
无效更新()
{
移动速度=0f;
if(Input.GetKeyDown(KeyCode.W)和&isground){
跳跃();
}
if(Input.GetKey(KeyCode.D)){
右();
}
if(Input.GetKey(KeyCode.A)){
左();
}
GetComponent().velocity=newvector2(moveVelocity,GetComponent().velocity.y);
anim.SetFloat(“速度”,Mathf.Abs(GetComponent().velocity.x));
anim.SetBool(“isground”,isground);
anim.SetBool(“攻击”,假);
if(Input.GetKeyDown(KeyCode.S)){
Atk();
}
if(GetComponent().velocity.x>0)
transform.localScale=新矢量3(1f、1f、1f);
else if(GetComponent().velocity.x<0)
transform.localScale=新矢量3(-1f,1f,1f);
}
我做了正确的功能来配合每一次按键,它们都工作得很好。我用UI按钮调用了这些函数,
Jump
Attack
都可以正常工作,但是
Left
Right
都不能工作

以下是我的按钮脚本的属性:

希望它能起作用:

public void OnPointerDown (PointerEventData eventData)
{
    btn_pressed = true;
}

public void OnPointerUp (PointerEventData eventData)
{
    btn_pressed = false;
}

if(btn_pressed)
{
    //code
{

使类用于处理按下的ui按钮。if语句转到Update()方法。