Hi!
Implemented the movement in the player using the rigidbody. It might not be the best option for this, but it was working. However, I was having some problems with crashing into walls. Player was stuck. So I added a Physics Material 2D to the player and it seemed to have resolved.
However, the player’s movement started to have a problem: After the collision, the player starts to move by himself without the need to press a key. Would anyone know the reason for this behavior?
1 or -1. That’s exactly what happens. For some reason, the Input doesn’t return to 0. But I have no idea why (It must be something related to 2D Material that has Frinction and Bounciness = 0).
If you have an analogue stick, GetAxisRaw almost never returns perfect 0 due to simple mechanical reasons, but usually it will be something more like 0.00001 or something small. 1 or -1 is not what I expected. This is kind-of a silly question, but you’re sure you don’t have a joystick upside-down the floor or something?
If I understand exactly what you asked, I’m just using the keyboard. But returning only an integer (-1, 0 or 1) seems normal for GetAxisRaw. Joystick I am not using as far as I know. Inputs didn’t change anything in Unity’s Horizontal and Vertical Inputs. This behavior is very strange. Seems to ignore physics (even though the character has a 2D rigidbody). I did some research and it seems that velocity can cause some problems when used with some physics material. I just can’t understand why and what causes it.
Try removing your if condition, the player may be moving because the rigidbody.velocity is not set back to zero because of the if condition when the player let go of the joystick.
I compiled a reduced version of this problem (The project was already big, and was still over 100MB). But I don’t know if it will be necessary. After all, with the help of the colleague below, I found the problem. Thanks for the help.
That’s exactly where the problem was. I didn’t know the details of how rigidbody works (what the documentation mentions is that it’s not good to use it for what I’m doing). A check is required as my player cannot walk in a crouch. Any suggestions on how to implement?
if (Vertical == -1) {
Rigidbody2D.velocity = Vector2.zero;
}
Is the player crouching whenever the down button is pressed? Then if that is the case, instead of resetting the whole velocity, which basically turn off gravity if you’re doing sidescrolling/platformer, you can just set the x property of the velocity and keep the y.
rigidbidy.velocity = new Vector2(0, rigidbidy.velocty.y);
Assuming that the game is not in topdown perspective and objects are affected by gravity.
Rigidbody is under influence of Newton’s law of motion, so unless you stop it, it will continue moving.
So if you set velocity to a horizontal value, the body will continue moving horizontally, unless it hits something. Vertical movement will be affected by gravity. And in your sceneario you never set horizontal component of movement to zero, because of the “Horizontal” check.