我无法通过单击按钮来移动播放器

时间:2020-08-30 05:03:55

标签: unity3d eventtrigger

enter image description here

我的玩家GameObject是AlienPlayer 我想通过单击按钮来移动播放器 代码在键盘控件上有效,但在单击按钮时无效 这是我的更新():

void Update()
{
    
    movement = Input.GetAxis ("Horizontal");
    if (movement > 0f) {
        
        left();
    }
    else if (movement < 0f) {
        
        right();
    }
    else {
      rigidBody.velocity = new Vector2 (0,rigidBody.velocity.y);
    }
    
    
    if (moveLeft) { left(); }
    else if (moveRight) { right(); }
    
}



public void left()
{
    
    rigidBody.velocity = new Vector2(movement*speed, rigidBody.velocity.y);
    //rigidBody.AddForce(transform.forward * speed, ForceMode2D.Force);
    transform.localScale = new Vector2(2f, 2f);

}
public void right()
{
    
    rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
    transform.localScale = new Vector2(-2f, 2f);

}

这是事件触发条件:

public void LeftBtnDown()
{
    moveLeft = true;
}
public void LeftBtnUp()
{
    moveLeft = false;
}
public void RightBtnDown()
{
    moveRight = true;
}
public void RightBtnUp()
{
    moveRight = false;
}

**我想为Android制作此控件**

1 个答案:

答案 0 :(得分:0)

您的代码似乎具有逻辑功能,我的第一个猜测是您的场景中没有任何事件系统。

您只需在层次结构菜单-> UI-> EventSystem中单击+,即可创建一个。

我不认为代码是问题所在,但是如果EventSystem最终不是问题所在,则可以尝试将Update方法更改为此:

void Update()
{

    movement = Input.GetAxis ("Horizontal");
    if (movement > 0f || moveLeft) {
        
        left();
    }
    else if (movement < 0f || moveRight) {
        
        right();
    }
    else {
      rigidBody.velocity = new Vector2 (0,rigidBody.velocity.y);
    }
    
}

如果这行不通,我需要其他信息来帮助您。

相关问题