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

问题描述

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制作此控件**

解决方法

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

您只需在层次结构菜单-> 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);
    }
    
}

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...