0
\$\begingroup\$

I want my player to deaccelerate when he stop not just stop in place. Here is my code:

public override Vector3 Move(MovementManager manager, Vector2 input, bool jump, bool isCrouching)
{
    float speed = isCrouching ? CrouchSpeed : MoveSpeed;
    Vector3 velocity = manager.Velocity;

    float currentVelocity = new Vector3(velocity.x, 0, velocity.z).magnitude;
    Vector3 moveDirection = Vector3.zero;

    if (Mathf.Abs(input.x) > 0 || Mathf.Abs(input.y) > 0)
    {
        moveDirection = new Vector3(input.x, 0, input.y);
        moveDirection = manager.transform.TransformDirection(moveDirection);

        if(input.y > 0)
        moveDirection *= input.y;

        LastMoveDirection = moveDirection;

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

        //if (currentVelocity > MaxSpeed) currentVelocity = MaxSpeed;
        if (currentVelocity > speed) currentVelocity = speed;
        return moveDirection.normalized * currentVelocity * Time.fixedDeltaTime;
    }
    else
    {
        currentVelocity -= Deacceleration * Time.fixedDeltaTime;
        currentVelocity = Mathf.Max(currentVelocity, 0);

        return LastMoveDirection.normalized * currentVelocity * Time.fixedDeltaTime;
    }
}
\$\endgroup\$
10
  • \$\begingroup\$ Maybe Deacceleration is too large? \$\endgroup\$ Commented Jun 7, 2022 at 4:22
  • \$\begingroup\$ I changed it to a smaller value but it still doesn't work. When I log the Deacceleration it seams it doesn't get the right value it is smaller. \$\endgroup\$ Commented Jun 7, 2022 at 7:06
  • \$\begingroup\$ Maybe some radical modification should be done to locate the problem, how about setting the Deacceleration to 0? It expects results that don't slow down. \$\endgroup\$ Commented Jun 7, 2022 at 7:14
  • \$\begingroup\$ Setting it to zero does nothing. \$\endgroup\$ Commented Jun 7, 2022 at 7:20
  • \$\begingroup\$ Ok I think the Deacceleration is working but there is a problem with movedirection vector \$\endgroup\$ Commented Jun 7, 2022 at 7:28

1 Answer 1

0
\$\begingroup\$

Here is the solution:

 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);

        if (input.y > 0)
            moveDirection *= input.y;

        LastMoveDirection = moveDirection;

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

        //if (currentVelocity > MaxSpeed) currentVelocity = MaxSpeed;
        if (currentVelocity > speed) currentVelocity = speed;

    }
    else if(input.x <= 0 && input.y <= 0)
    {
        currentVelocity -= Deacceleration * Time.fixedDeltaTime;
        currentVelocity = Mathf.Max(currentVelocity, 0);
    }

    return LastMoveDirection.normalized * currentVelocity * Time.fixedDeltaTime;

}

There was the problem with moveDirection and manager.velocity these 2 vectors were zero at a specific point so the player just stop in place. Deacceleration was working but it was multiply by the zero vector so it did nothing.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.