How can I make my player stop when changing direction?

I want my player to stop for a split of a second when changing direction instead of just continuing to move. I control my player with virtual joystick ( it is an androidn game) and currently I have a behavior where your speed is determent partly by position of a joystick ( the closer it is to the middle you move slower). The problem is I can still change direction rally fast so my player doesn’t slow down when I start moving backwards after moving forward. Here is my code:

    public override Vector3 Move(MovementManager manager, Vector2 input, bool jump, bool isCrouching)
    {
        float speed = isCrouching ? CrouchSpeed : MoveSpeed;
        Input = input;
        if (Mathf.Abs(input.x) > 0 || Mathf.Abs(input.y) > 0)
        {
            moveDirection = new Vector3(input.x, 0, input.y);
            moveDirection = manager.transform.TransformDirection(moveDirection);


            LastMoveDirection = moveDirection;

            if (IsWalkSpeed) currentVelocity += (Acceleration * .4f) * Time.fixedDeltaTime;
            else currentVelocity += Acceleration * Time.fixedDeltaTime;

            if (currentVelocity > speed) currentVelocity = speed;
           
            currentVelocity *= Mathf.Abs(input.y) + Mathf.Abs(input.x);

        }
        else
        {
            currentVelocity -= Deacceleration * Time.fixedDeltaTime;
            currentVelocity = Mathf.Max(currentVelocity, 0);
        }

        return LastMoveDirection.normalized * currentVelocity * Time.fixedDeltaTime;
    }

maybe could check with .dot, how much direction has changed,

Hm not sure how to use it correctly in my case but I will try something out.

I don’t think this helps much in my case.

You could safe your last direction into a variable and check if its still the same. If not start a Coroutine with N seconds that prevents your movement or that amount of time.

Something like that could work will give it a try.

Still didn’t figure it out so bump!

so i was thinking that with vector3.dot, you could get useful value from these to vectors:
8220246--1073445--upload_2022-6-20_22-52-46.png

“Dot returns 1 if they point in exactly the same direction,
-1 if they point in completely opposite directions and
zero if the vectors are perpendicular”

so maybe use that value as delay or turn speed multiplier or something similar.

Hm I didn’t think about it in a way of applying it to joystick itself I will try that tnx.

How much is your “change direction” exactly?

More like 1 or 2:

8220366--1073460--upload_2022-6-20_22-54-39.png

Red is the area that will count as none changed directions while the black dot is your stick

I don’t understand what you mean by how much is it? If you mean the values of input it goes from 1 to -1 but if I applay Mathf.Abs than it is 1 to 1

The black dot is the position of your stick on the pad.
The red area is what is still inside of your direction of stick.

So if you, for example, push your joystick stick on the right top, and afterwards to the center top. Is that still the same direction or already a new one?

I ask that because if you are only looking for the opposite direction as a different direction, you could just negate your input and use a distance check as tolerance.

8221623--1073706--upload_2022-6-21_12-51-53.png

In that case, if your stick goes from the upper right towards the yellow area, you would be in a new direction.

Of course you need a buffer N long of your last Inputs per frame to check the difference of input per frame.

1 Like

if it is above center like in first picture the player goes forward and if it is below like in second one the player goes backwards. So I know when direction changes. The rotation doesn’t really affect the direction.
8221644--1073718--upload_2022-6-21_13-0-44.png8221644--1073721--upload_2022-6-21_13-1-4.png

Ok, so what you could do is basically what i already kinda suggested.

You keep a buffer of Information, like the last N frames you want it to detect the changes.

Like you most likely already know your input will most likely look like that:
8222097--1073799--upload_2022-6-21_15-28-22.png

Starting from 0 going frame to frame down to 4.
So you, in this example, buffer an array of 4 values.
You always check the first value in your list and in the end remove the first and replace it with the second in line, and so on.

Now depends on what you wanna achive. Lets suggest you always want a direction change if the stick is on the circle line. Which is kinda simple since the sum will always be 1 or the negative of you start input.

8222097--1073802--upload_2022-6-21_15-34-36.png

So lets say you just want this.
8222097--1073805--upload_2022-6-21_15-36-59.png

Going into red. Like you can see you will always be the negative of your start index. You should also see that you are trying to cross 0 or half your sum. So you go through your buffer and check if your current stick position is the negative value of your start index.

So with the buffer example above:

Your buffer: [x = 0, y = 1], [x = 0, y = 0.5] [x = 0, y = 0] [x = 0, y = -0.5] [x = 0, y = -1]

So you check your index 0 which is [x = 0, y = 1] and your current position, which is [x = 0, y = -1].
So you check the diff of your X values and Y values.
X: 0 and Y: 2, diff of 2.

If your stick goes from top to right:
With Index 0: [x = 0, y = 1] and Index 4: [x = 1, y = 0]
You got X = 1 and Y = 1, together a diff of 2

If your stick goes from top in the middle
With Index 0: [x= 0, y= 1] and Index 4: [x= 0, y= 0]
You got X = 0, Y= 1, together a diff of 1.

So you need at least a diff of 1 to change your direction.

Thanks for detailed explanation I can’t test this before tomorrow but I will let you know if I mange to do it.