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. Dismiss Notice

Movement slower when on ground, faster when jumping

Discussion in '2D' started by LQPewds, Jul 10, 2021.

  1. LQPewds

    LQPewds

    Joined:
    Nov 12, 2020
    Posts:
    25
    Hi, I am coding a game and I used Brackeys Character Controller tutorial. After finishing it i started the animation and did all of it. All of the animations work just fine but there is a problem with the movement. When I run on the ground it's pretty slow, slower than before I did the animation, but whenever I jump then it accelerates and then slows back down when I'm on the ground.

    My Code:
    Code (CSharp):
    1. sing System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.  
    8.     public CharacterController2D controller;
    9.  
    10.         public float runSpeed = 40f;
    11.  
    12.     public Animator animator;
    13.  
    14.     float horizontalMove = 0f;
    15.     bool jump = false;
    16.     void Update()
    17.     {
    18.         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    19.  
    20.         animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
    21.  
    22.         if (Input.GetKey(KeyCode.Space))
    23.         {
    24.             jump = true;
    25.             animator.SetBool("IsJumping", true);
    26.         }
    27.     }
    28.  
    29.     public void OnLanding()
    30.     {
    31.         animator.SetBool("IsJumping", false);
    32.     }
    33.  
    34.     void FixedUpdate()
    35.     {
    36.         controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
    37.         jump = false;
    38.     }
    39. }
    The Animation Tutorial:


    The Character Tutorial:
     
  2. nathukadevyt

    nathukadevyt

    Joined:
    Mar 28, 2021
    Posts:
    17
    I know this is quite an old question, but this could be caused by the Physic Material interaction between your player and ground. If there's too much friction, your player will move slower. And it is moving in the air faster because there is no longer any interactions between player and another Physic Material.

    If any of the above made absolutely no sense, then just tell me and I can send a picture-by-picture walkthrough (If that is definitely the problem)
     
  3. LQPewds

    LQPewds

    Joined:
    Nov 12, 2020
    Posts:
    25
    As you said i didnt really understand so i would 100% appreaciate a walkthrough
     
  4. nathukadevyt

    nathukadevyt

    Joined:
    Mar 28, 2021
    Posts:
    17
    1: Create 2 New "2D Physic Materials" (Right Click On Project Window, Go Create ---> (Scroll Down) Physic Material 2D)
    2: Name 1 "Player", And Name The Other "Ground"
    3: On The "Player" Material, Open It Up And You Should See Something Like This:
    4: Make The Friction At A Moderate Number. I'd say around 0.7 - 1.5, but you can always change this later. The key is, Higher the number, the Slower the player will move when moving upon Ground, Lower The Number, the faster the player will go. Once You Have Chosen A Number, Press Enter.
    5: Now Open The "Ground" Material, And Do The Same As You Did With The "Player" Material But Instead, Make The Friction Quite Low (Around 0.2 - 0.7). Once Again, You Can Always Change This Number Later.
    6: Once You Have Saved Both Physics Materials, You Must Assign Them To The gameObject that will be making the collision. Assign The "Ground" physic Material to the gameObject that acts as your ground, do this by inserting the Physics Material into both the Rigidbody 2D & the Shape Collider 2D (Square, Circle, Cone, Triangle etc.) Where it says "Material". Do the same with your "Player" gameObject and assign the "Player" physics.



    Once You've Done This, Playtest Your Game And Adjust The "Friction" Setting As You Like! If there was anything here that still doesn't make sense, just tell me and I can go further into depth.
     
    tsirimak and Akcl7777 like this.
  5. Akcl7777

    Akcl7777

    Joined:
    Jul 28, 2022
    Posts:
    1
    You are a life saver dude