Deceleration not working, I know the problem but I don't know how to fix it

using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Rendering;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody2D rb;
public Transform groundCheck;
public LayerMask groundLayer;

public float horizontal;
private float speed = 8f;
private float jumpPower = 14f;
private bool isFacingRight = true;
private float speedMultiplier;
private bool buttonPressed;
private float targetSpeed;

[Range(1, 10)]
[SerializeField] float acceleration;

// Update is called once per frame
void Update()
{
    
    UpdateSpeedMultiplier();
    rb.velocity = new Vector2(horizontal * targetSpeed, rb.velocity.y);

    if (!isFacingRight && horizontal > 0f)
    {
        Flip();
    }
    else if (isFacingRight && horizontal < 0f)
    {
        Flip();
    }
}

public void Jump(InputAction.CallbackContext context)
{
    if (context.performed && isGrounded())
    {
        rb.velocity = new Vector2(rb.velocity.x, jumpPower);
    }

    if (context.canceled && rb.velocity.y > 0f)
    {
        rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
    }
}

private bool isGrounded() 
{
    return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);

}

private void Flip()
{
    isFacingRight = !isFacingRight;
    Vector3 localScale = transform.localScale;
    localScale.x *= -1f;
    transform.localScale = localScale;
}


public void Move(InputAction.CallbackContext context)
{
    
    horizontal = context.ReadValue<Vector2>().x;
    
    if (horizontal != 0f)
    {
        buttonPressed = true;
    }
    else
    {
        buttonPressed = false;
    }
}

void UpdateSpeedMultiplier()
{
    if (buttonPressed && speedMultiplier < 1)
    {
        speedMultiplier += Time.deltaTime * acceleration;
        
    }
    else if (!buttonPressed && speedMultiplier > 0)
    {
        speedMultiplier -= Time.deltaTime * acceleration;
        if (speedMultiplier < 0) speedMultiplier = 0;
    }
}

}

If you know the problem, it’s really kinda weird that you didn’t say even one single word about it.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

Also, review how to post code: the above code is damaged by mis-posting.

If you post code, only post the relevant code and always use the format button above. Do not post photographs of code.

If you just have a bug, here’s how to reason about it:

Time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

Hi
thanks for the advice!
This was my first time using the unity forums, and i should have been more clear about the problem and the code. The problem is that for the movement, the horizontal variable is showing which way the player is moving, -1, 0, 1. it is being multiplied by the target speed variable, which gradually increases until it hits the max speed. the horizontal variable is only changing gradually from 0 to 1 when acceleration is happening. during the deceleration, there is none because it is just set to 0. i hoped this helped, but i am new to unity and coding in general, so i am still trying to figure this out.

I think to make it work i would need to set it to 1 until the speedmultiplier variable is zero, but i am unsure how to go about doing this. the horizontal variable must still be able to be set to -1, so the player is able to go both right and left.

It sounds like you’re just trying to change a quantity (speed) over time, rather than instantaneously, and have it work either slowing down or speeding up. Is that the question?

If so…

Smoothing a change between any two particular values:

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub