Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question player keeps momentum after landing jump

Discussion in 'Physics' started by nirrow, Jul 11, 2020.

  1. nirrow

    nirrow

    Joined:
    Jul 4, 2020
    Posts:
    1
    hello,
    I made a script for my game that stops the player and charges jump when space button is pressed and when I release it the player jumps in a predefined way (just like Jumpking) but from some reason when the player object lands instead of stopping on spot it lands it keeps some momentum from the jump and slide on the x-axis and I can't really figure out why is it happening. I tried to stop the player with Oncollisionenter but it did not really work and I'm not sure how would the code have to look like.

    thanks for every idea
    Code (CSharp):
    1. public class jumpkingJump : MonoBehaviour
    2. {
    3.     //groundcheck variables
    4.     private bool Grounded;
    5.     public LayerMask WhatIsGround;
    6.     private Transform GroundCheck;
    7.     private float GroundedRadius = .2f;
    8.  
    9.     //jump variables
    10.     private float JumpPressure = 0f;
    11.     public float minJump = 4f;
    12.     public float maxjump = 20f;
    13.     private float StopSpeed;
    14.  
    15.     //movement
    16.     public float moveSpeed = 5f;
    17.     public const string RIGHT = "right";
    18.     public const string LEFT = "left";
    19.     public const string MIDDLE = "middle";
    20.     string ButtonPressed;
    21.  
    22.  
    23.     private Rigidbody2D m_Rigidbody2D;
    24.  
    25.  
    26.     // Update is called once per frame
    27.  
    28.     private void Awake()
    29.     {
    30.         GroundCheck = transform.Find("GroundCheck");
    31.         m_Rigidbody2D = transform.GetComponent<Rigidbody2D>();
    32.     }
    33.  
    34.     void FixedUpdate ()
    35.     {
    36.         //check if player is standing on the ground
    37.         Grounded = false;
    38.         Collider2D[] colliders = Physics2D.OverlapCircleAll(GroundCheck.position, GroundedRadius, WhatIsGround);
    39.         for (int i = 0; i < colliders.Length; i++)
    40.         {
    41.             if (colliders[i].gameObject != gameObject)
    42.                 Grounded = true;
    43.  
    44.         }
    45.  
    46.     }
    47.     void Update()
    48.     {
    49.         if (Grounded)
    50.         {
    51.             //movement right
    52.             if (Input.GetKey(KeyCode.D))
    53.             {
    54.                 ButtonPressed = RIGHT;
    55.                 transform.Translate(Vector2.right * (Time.deltaTime * moveSpeed));
    56.  
    57.             }
    58.  
    59.             //movement left
    60.             else if (Input.GetKey(KeyCode.A))
    61.             {
    62.                 ButtonPressed = LEFT;
    63.                 transform.Translate(Vector2.left * (Time.deltaTime * moveSpeed));
    64.  
    65.             }
    66.             else
    67.             {
    68.                 ButtonPressed = MIDDLE;
    69.             }
    70.         }
    71.  
    72.         //Jump if grounded
    73.         if (Grounded)
    74.         {
    75.             if (Input.GetButton("Jump"))
    76.             {
    77.                 StopSpeed = moveSpeed;
    78.                 moveSpeed = 0f;
    79.                 if(JumpPressure < maxjump)
    80.                 {
    81.                     JumpPressure += Time.deltaTime * 15f;
    82.                 }
    83.                 else
    84.                 {
    85.                     JumpPressure = maxjump;
    86.                 }
    87.                
    88.             }
    89.             //release button
    90.             else
    91.             {
    92.                 //jump//
    93.                 if (JumpPressure > 0f)
    94.                 {
    95.                     //deciding which trajectory depending on variable Button Pressed
    96.                     if (ButtonPressed == RIGHT)
    97.                     {
    98.                         JumpPressure = JumpPressure + minJump;
    99.                         m_Rigidbody2D.velocity = new Vector2(8f, JumpPressure);
    100.                     }
    101.                     else if (ButtonPressed == LEFT)
    102.                     {
    103.                         JumpPressure = JumpPressure + minJump;
    104.                         m_Rigidbody2D.velocity = new Vector2(-8f, JumpPressure);
    105.                     }
    106.                     else
    107.                     {
    108.                         JumpPressure = JumpPressure + minJump;
    109.                         m_Rigidbody2D.velocity = new Vector2 (0f, JumpPressure);
    110.                     }
    111.                     JumpPressure = 0f;
    112.                     moveSpeed = 5;
    113.                    
    114.                 }
    115.             }
    116.  
    117.         }
    118.     }
    119.     void OnCollisionEnter2D(Collision2D col)
    120.     {
    121.         if(col.gameObject.tag == "ground")
    122.         {
    123.             m_Rigidbody2D.velocity = Vector2.zero;
    124.         }
    125.     }
    126. }
     
  2. noyogiisMYusername

    noyogiisMYusername

    Joined:
    May 18, 2020
    Posts:
    21
    Not sure this is your problem, or whether it is the appropriate solution.
    Do you have a physics2d material on your feet collider, that could give you some grip.
    Then if you are sticking to the walls you could add another collider that is slippery to help you slide down the walls, this could be on empty object child of your player. If this is bad method hopefully someone else will chime in.
    I am using capsule collider2d horizontal for my feet collider, with a physics2d material.
     
    nirrow likes this.