简体   繁体   English

如何在Unity2D中更改播放器的水平移动方向?

[英]How to change the horizontal movement direction of the player in Unity2D?

I am trying to make a 2D game in Unity and I am using in FixedUpdate() function the Input.GetMouseButtonDown() method. 我试图在Unity中制作2D游戏,并且在FixedUpdate()函数中使用Input.GetMouseButtonDown()方法。 I want my player to change the horizontal direction, so for this I have the below code. 我希望我的播放器改变水平方向,因此我有以下代码。

 if (Input.GetMouseButtonDown(0))
             {
                 if(change == true)
                 {
                     rb.velocity = new Vector2(-12,rb.velocity.y);
                     change=!change;
                 }
                 else if(change == false)
                 {
                     change=!change;
                     rb.velocity = new Vector2(12,rb.velocity.y);
                 }
     }

At the beginning, first 3-6 clicks, works fine (one click change direction, another click the other direction) but afterwards I have to press 2 or 3 times to change the actual direction. 刚开始时,先单击3-6次即可正常工作(一个单击更改方向,另一个单击另一个方向),但此后我必须按2或3次才能更改实际方向。

What should I do to increase the accuracy, quality of changing directions? 我该怎么做才能提高指示方向的准确性和质量?

Thank you very much for your patience and attention! 非常感谢您的耐心和关注!

The Unity Documentation states that you have to use GetMouseButtonDown() in the Update function. Unity文档指出,您必须在Update函数中使用GetMouseButtonDown()。

You probably should create a global boolean to save the value and reset it in the FixedUpdate(). 您可能应该创建一个全局布尔值以保存该值,然后在FixedUpdate()中将其重置。 Something like this: 像这样:

boolean MouseButtonDown=false;
void Update(){
if(Input.GetMouseButtonDown(0)){
MouseButtonDown=true;
}
}

void FixedUpdate(){
if (MouseButtonDown)
             {
                 if(change == true)
                 {
                     rb.velocity = new Vector2(-12,rb.velocity.y);
                     change=!change;
                 }
                 else if(change == false)
                 {
                     change=!change;
                     rb.velocity = new Vector2(12,rb.velocity.y);
                 }
     }
}

The FixedUpdate() function runs after a fixed interval of time, if you click the mouse button at the time when if(Input.GetMouseButtonDown(0) is executing then your input is taken into account. The Update() function runs for every frame that is being displayed on the screen, if your fps (frame rate) is 60fps it means that the Update() function runs 60 times per second so there's a very low possibility that your input isn't recorded. Hope that answers why your code isn't working. FixedUpdate()函数在固定的时间间隔后运行,如果在执行if(Input.GetMouseButtonDown(0)时单击鼠标按钮,则将您的输入考虑在内if(Input.GetMouseButtonDown(0) Update()函数每帧运行一次这是在屏幕上显示的,如果您的fps(帧速率)为60fps,则意味着Update()函数每秒运行60次,因此记录输入的可能性很小。希望回答您的代码的原因不起作用。

What you can do is: 您可以做的是:

bool btnPressed = false;
void Update(){

    if(Input.GetMouseButton(0) && !btnPressed){
        btnPressed = true;
    }

}

void FixedUpdate(){

    if(btnPressed){

        if(change == true){
            rb.velocity = new Vector2(-12,rb.velocity.y);
            change=!change;
        }
        else if(change == false){
            change=!change;
            rb.velocity = new Vector2(12,rb.velocity.y);
        }

        btnPressed = false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM