Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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
Unity3d 我能';不要通过点击按钮来移动我的播放器_Unity3d_Eventtrigger - Fatal编程技术网

Unity3d 我能';不要通过点击按钮来移动我的播放器

Unity3d 我能';不要通过点击按钮来移动我的播放器,unity3d,eventtrigger,Unity3d,Eventtrigger,我的玩家游戏对象是AlienPlayer 我想通过点击按钮移动播放器 代码在键盘控件上工作,但不在单击按钮时工作 这是我的更新(): void Update() { movement = Input.GetAxis ("Horizontal"); if (movement > 0f) { left(); } else if (movement < 0f) {

我的玩家游戏对象是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不是问题所在,您可以尝试将更新方法更改为:

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);
    }
    
}
void Update()
{
移动=Input.GetAxis(“水平”);
如果(移动>0f | |向左移动){
左();
}
else if(移动<0f | |向右移动){
对();
}
否则{
rigidBody.velocity=新矢量2(0,rigidBody.velocity.y);
}
}

如果这不起作用,我需要更多的信息来帮助您。

EventSystem在场景中,我按照您在代码中所说的做了,询问我可能得到此结果的注释我将首先调试LeftBtnDown、LeftBtnUp、RightBtnDown和RightBtnUp方法,以确保它们被调用(只需调用一个Debug.Log,每个函数中都有一条特定的消息)同时,检查是否没有其他UI对象阻止按钮,例如另一个画布(我不能100%确定它们是否可以接受输入)或者其他不可见的元素。如果有其他UI阻止它们,我会尝试禁用它们,看看按钮现在是否工作。