Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question How to make my charater to gain speed slower?

Discussion in 'Scripting' started by Kokomeant, May 4, 2023.

  1. Kokomeant

    Kokomeant

    Joined:
    Dec 23, 2022
    Posts:
    13
    Hello.

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

    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     private Rigidbody2D Rb2D;
    8.     private Transform legs;
    9.  
    10.     private float inputX;
    11.  
    12.     [Header("Character Parameters")]
    13.  
    14.     [SerializeField] private float speed = 2;
    15.     [SerializeField] private float jumpPower = 14f;
    16.     [SerializeField] private float maxJumpPower = 16f;
    17.     [SerializeField] private bool onJump = false;
    18.     [SerializeField] private Vector2 lastVelocity;
    19.     [SerializeField] private float drag = 0.076f;
    20.  
    21.     [Header("Ground check settings")]
    22.  
    23.     [SerializeField] private float _radius = 0.39f;
    24.     private const int layerMask = 1 << 6;
    25.  
    26.  
    27.     private void Awake() {
    28.         Rb2D = GetComponent<Rigidbody2D>();
    29.         legs = transform.Find("legs");
    30.     }
    31.     private void Update() {
    32.         ApplyInput();
    33.         lastVelocity = Rb2D.velocity;
    34.     }
    35.     private void FixedUpdate() {
    36.         ApplyMovement();
    37.  
    38.     }
    39.     private void ApplyInput()
    40.     {
    41.         inputX = Input.GetAxisRaw("Horizontal");
    42.  
    43.         if (Input.GetKey(KeyCode.Space) && onGrounded()) {
    44.             onJump = true;
    45.         }
    46.     }
    47.     private void OnCollisionEnter2D(Collision2D collision)
    48.     {
    49.         if (collision.gameObject.CompareTag(UtilsClass.Tags.Wall.ToString())) {
    50.             Rb2D.velocity = new Vector2(-lastVelocity.x, Rb2D.velocity.y);
    51.         }
    52.     }
    53.     private void ApplyMovement() {
    54.  
    55.         Rb2D.AddForce(Vector2.right * inputX, ForceMode2D.Impulse);
    56.  
    57.         if (inputX > 0) {
    58.             Rb2D.AddForce(Vector2.right/10f, ForceMode2D.Impulse);
    59.         }
    60.         else if (inputX < 0) {
    61.             Rb2D.AddForce(-Vector2.right/10f, ForceMode2D.Impulse);
    62.         }
    63.  
    64.  
    65.         Vector2 vel = Rb2D.velocity;
    66.  
    67.         if (Mathf.Abs(inputX) == 1)  {
    68.             if (Mathf.Abs(Rb2D.velocity.x) >= 15f) {
    69.                 vel.x *= 1f - drag;
    70.             }
    71.             else {
    72.                 vel.x *= 1f;
    73.             }
    74.         }
    75.         else {
    76.             vel.x *= 1f - drag;
    77.         }
    78.  
    79.         Rb2D.velocity = new Vector2(vel.x, Rb2D.velocity.y);
    80.  
    81.         if (onJump) {
    82.            float totalJumpForce = Mathf.Abs(Rb2D.velocity.x) + jumpPower;
    83.            Rb2D.AddForce(Vector2.up * totalJumpForce, ForceMode2D.Impulse);
    84.            onJump = false;
    85.         }
    86.         if(Rb2D.velocity.y >= maxJumpPower) {
    87.             Rb2D.velocity = new Vector2(Rb2D.velocity.x, maxJumpPower);
    88.         }
    89.     }
    90.  
    91.     private bool onGrounded() {
    92.         return Physics2D.OverlapCircle(legs.position, _radius, layerMask);
    93.     }
    94. }
    95.  
    Maybe there is another technique to make the character gain speed slower? How I can make my character gain speed slower?

    Thanks in advance :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,946
    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:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    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: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4
     
    seejayjames and Kokomeant like this.
  3. Kokomeant

    Kokomeant

    Joined:
    Dec 23, 2022
    Posts:
    13
    Thank you very much!, It helped me a lot! I really appreciate your help. :)
     
    Kurt-Dekker likes this.