How to make my charater to gain speed slower?

Hello.

First of all, I want to apologize for my bad English I hope I explained myself well. :slight_smile:

I started to work on a game that you need to climb up higher as possible and faster you walk than higher you will jump and also the walls will apply reversed force when you hit them, I can say that I achieved the character controller that I need for my game but I have one problem that my character gains speed very fast.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private Rigidbody2D Rb2D;
    private Transform legs;

    private float inputX;

    [Header("Character Parameters")]

    [SerializeField] private float speed = 2;
    [SerializeField] private float jumpPower = 14f;
    [SerializeField] private float maxJumpPower = 16f;
    [SerializeField] private bool onJump = false;
    [SerializeField] private Vector2 lastVelocity;
    [SerializeField] private float drag = 0.076f;

    [Header("Ground check settings")]

    [SerializeField] private float _radius = 0.39f;
    private const int layerMask = 1 << 6;


    private void Awake() {
        Rb2D = GetComponent<Rigidbody2D>();
        legs = transform.Find("legs");
    }
    private void Update() {
        ApplyInput();
        lastVelocity = Rb2D.velocity;
    }
    private void FixedUpdate() {
        ApplyMovement();

    }
    private void ApplyInput()
    {
        inputX = Input.GetAxisRaw("Horizontal");

        if (Input.GetKey(KeyCode.Space) && onGrounded()) {
            onJump = true;
        }
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag(UtilsClass.Tags.Wall.ToString())) {
            Rb2D.velocity = new Vector2(-lastVelocity.x, Rb2D.velocity.y);
        }
    }
    private void ApplyMovement() {

        Rb2D.AddForce(Vector2.right * inputX, ForceMode2D.Impulse);

        if (inputX > 0) {
            Rb2D.AddForce(Vector2.right/10f, ForceMode2D.Impulse);
        }
        else if (inputX < 0) {
            Rb2D.AddForce(-Vector2.right/10f, ForceMode2D.Impulse);
        }


        Vector2 vel = Rb2D.velocity;

        if (Mathf.Abs(inputX) == 1)  {
            if (Mathf.Abs(Rb2D.velocity.x) >= 15f) {
                vel.x *= 1f - drag;
            }
            else {
                vel.x *= 1f;
            }
        }
        else {
            vel.x *= 1f - drag;
        }

        Rb2D.velocity = new Vector2(vel.x, Rb2D.velocity.y);

        if (onJump) {
           float totalJumpForce = Mathf.Abs(Rb2D.velocity.x) + jumpPower;
           Rb2D.AddForce(Vector2.up * totalJumpForce, ForceMode2D.Impulse);
           onJump = false;
        }
        if(Rb2D.velocity.y >= maxJumpPower) {
            Rb2D.velocity = new Vector2(Rb2D.velocity.x, maxJumpPower);
        }
    }

    private bool onGrounded() {
        return Physics2D.OverlapCircle(legs.position, _radius, layerMask);
    }
}

Maybe there is another technique to make the character gain speed slower? How I can make my character gain speed slower?

Thanks in advance :slight_smile:

Instead of directly setting the speed, you would set a DESIRED speed, then move the CURRENT speed towards that.

This way the velocity won’t change instantly. Here’s more reading:

Smoothing movement 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

2 Likes

Thank you very much!, It helped me a lot! I really appreciate your help. :slight_smile:

1 Like