用于使角色左右移动的Unity UI按钮不起作用

时间:2015-11-16 11:34:00

标签: c# unity3d

我正在为手机制作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);

}

我做了正确的功能,按下每个按键,它们都工作得很好。我使用UI按钮调用这些函数,JumpAttack工作正常,但LeftRight不起作用。

以下是我的Button脚本的属性:

Button (Script)

1 个答案:

答案 0 :(得分:0)

希望它有效:

CREATE FUNCTION v_gameTypes
(@game_ID int)
RETURNS TABLE
AS
BEGIN
IF( EXISTS(SELECT*FROM Strategies WHERE Strategies.game = @game_ID))
RETURN  SELECT Strategies.real_time FROM Strategies WHERE Strategies.game = @game_ID

IF( EXISTS(SELECT*FROM Actions WHERE Actions.game = @game_ID))
RETURN  SELECT Actions.sub_genre FROM Actions WHERE Actions.game = @game_ID

IF( EXISTS(SELECT*FROM Sports WHERE Sports.game = @game_ID))
RETURN  SELECT Sports.s_type FROM Sports WHERE Sports.game = @game_ID

RETURN  SELECT RPGs.story_line, RPGs.PvP FROM RPGs WHERE RPGs.game = @game_ID
END
GO