Weird issue

Hi!

Here is not the best place to talk about this, but i am just involving more people…

I had a simple custom node that is “movement alpha”. The function of this node is to provide a direction for the character:screenshot1

As you can see, there is four boolean inputs. You can put WASD in this inputs and the node will give you a direction in vector to be multiplied by the velocity you want.

The problem is: if you press A + D, the character will not move (because the alpha of X will be 0), but if you press W or S together with A+D, the character will have the double of the velocity.

I really don’t know what is happening here. There is a Canvas element to debug the given vector and it is ok (never is 2 for example). The bug also happens with other keys and also is present even in the viewport camera.

If someone have a opinion of what is causing this bug, i could better formulate an issue for Kha.

There is the test files with the node: movement_alpha.zip

Thanks in advance.

but if you press W or S together with A + D, the character will have the double of the velocity
If you look at examples of such algorithms (https://docs.godotengine.org/ru/stable/tutorials/2d/2d_movement.html), then we can say that there is not enough normalization for the direction.
The result is like this (print value = direction * speed):

Suggested Code:

    if (forward == true) direction.y + = 1;
    if (backward == true) direction.y + = -1;

    if (left == true) direction.x + = -1;
    if (right == true) direction.x + = 1;

    return direction.normalize ();

Alternatively, you can add the speed value as a parameter:
modif

Thanks, i had tried to use normalize as an math operation instead of a function :confused:

Also thanks for your suggestion. I will also add a state input. The velocity will vary with the character state (crouching, running, proning, etc.)

But i still can’t figure what is happening when you press A+D and W at the same time. The character is going faster forward. Also using Vec3 is not changing this behavior.

The point is the Merge node, because of it, it turns out that In-Out passes twice or three times (due to parallel processing of input) and the Translate Object node is triggered twice or three times, so the movement is faster.
This needs to be reorganized. I made an example, it’s not optimal, but working:
work.zip

P.S. Normalization operation does not look correct in the Vector Math node, because this node needs 2 inputs and normalization is done on one vector.

Indeed. I tough as Merge node have a single output, only one Translate node should be activated. Maybe we need a new node to do that or to fix this behavior, what do you think? My opinion is yes, because there is not a way to execute this function without a update node checking for a true boolean (wich will introduce lag and nodes disorganization).

Here is the shortest workaround possible on this:

Other one is always apply the translate using On Update node. This is the best way, but when there is no input pressed will have a translate node with 0 value.

The second bug that persists is the character moving even without a valid vector. This one is hidden very well.

We need a node to check the state on demand, that is, when updating, ask the state of a particular button. In this case, everything will be processed in the update stream and not interfere with each other.

I don’t quite understand what the mistake is. More details, please

It turns out that everything you need is already there, only the state can be requested in the Keyboard node. As a result, your code can be shortened further:
version

I will try to explain the bug, but it is a bit confuse to understand, you will need to test with your own hands.

If you press A: x = -1
D: x = 1
result = 0

The same goes for W and S. The problem is: when you press A+D , the player will not move in the X vector. But if you, at the same time, press W + S, the player shouldn’t move. But this is not happening.

Just press A + D and after press W + S at the same time (the player should be not moving as X and Y vectors are equal to 0).

I am just trying to understand from where this bug is coming as the vector in canvas is 0, 0, 0 and the player keeps moving…

In the current version that I described above, I have:

  • A + D - the player does not move;
  • W + S - the player does not move;
  • W + A + S + D - the player does not move.

The Merge node only obfuscates the logic. Input processing should be performed in one thread, preferably in Update, and not in parallel with several events - this complicates and creates errors.

Then maybe the problem is only happening in Linux. I will test in Windows sometime. Now i am trying to make a snake game and it is more complicated than i expected :slight_smile: I will send it in the another topic if i finish.

Thanks for the extra information here :+1:

1 Like