A
A
AnzousDGC2019-10-17 10:22:13
C++ / C#
AnzousDGC, 2019-10-17 10:22:13

How to properly implement character movement in Unity 2D?

I ask for your help. I'm developing a 2D platformer. I don’t understand how to implement the correct repulsion of a character from a bomb explosion - he is repelled correctly only along the Y axis, the X effect is very weak (the explosion is implemented using the point effector according to the official guide)
After reviewing the character movement code, I found the reason for this behavior - velocity.x every frame is set to 0 if the player is not moving. But no matter how I tried to fix it, the character either behaves incorrectly, or starts sliding non-stop in the direction of movement.
I can’t attach the code, but I’ll briefly describe it: the character moves with the help of velocity, which is performed non-stop in FixedUpdate. The variable dir is used in velocity (sets the direction) and if the character stops, then dir=0.
Tried to enter a bool variable, but the character starts to slide. If the character moves and falls within the radius of the explosion, he is correctly knocked back, otherwise if he stands still, nothing happens, or he is thrown a little to the side and sharply upwards.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2019-10-17
@Griboks

Your movement is implemented through coordinates, and repulsion through forces. Use one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question