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

Player Bounces Down Hills

Discussion in '2D' started by ZachAR3, Jan 27, 2019.

  1. ZachAR3

    ZachAR3

    Joined:
    Jan 27, 2019
    Posts:
    5
    Hi so my player bounces down hills i use rigid body and my movement script i am still pretty new so the simpler the better heres my code TIA

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.     Rigidbody2D rb;
    7.  
    8.     [SerializeField] private float runSpeed = 5f;
    9.     [SerializeField] private float fallMultiplier = 2.5f;
    10.     [SerializeField] private float lowJumpMultiplier = 2f;
    11.  
    12.     float horizontalMove;
    13.     bool isGrounded;
    14.  
    15.  
    16.    // Use this for initialization
    17.    void Start ()
    18.     {
    19.         rb = GetComponent<Rigidbody2D>();
    20.    }
    21.  
    22.     private void OnCollisionStay2D(Collision2D collision)
    23.     {
    24.         isGrounded = true;
    25.         rb.gravityScale = 0;
    26.     }
    27.  
    28.     private void OnCollisionExit2D(Collision2D other)
    29.     {
    30.         rb.gravityScale = 3.5f;
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     private void Update()
    35.     {
    36.  
    37.         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    38.  
    39.         if (Input.GetButtonDown("Jump") && isGrounded)
    40.         {
    41.             rb.AddForce(Vector3.up * 25, ForceMode2D.Impulse);
    42.         }
    43.  
    44.         if (Input.GetButton("Sprint") && horizontalMove >= 1)
    45.         {
    46.             horizontalMove += 10;
    47.         }
    48.  
    49.         else if (Input.GetButton("Sprint") && horizontalMove <= -1)
    50.         {
    51.             horizontalMove -= 10;
    52.         }
    53.  
    54.     }
    55.  
    56.     private void FixedUpdate()
    57.     {
    58.         transform.Translate(horizontalMove * Time.fixedDeltaTime, 0, 0);
    59.  
    60.         if (rb.velocity.y < 0)
    61.         {
    62.             isGrounded = false;
    63.             rb.AddForce(Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.fixedDeltaTime, ForceMode2D.Impulse);
    64.         }
    65.         else if (rb.velocity.y > 0 && !Input.GetButton("Jump"))
    66.         {
    67.             isGrounded = false;
    68.             rb.AddForce(Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.fixedDeltaTime, ForceMode2D.Impulse);
    69.         }
    70.  
    71.     }
    72.  
    73. }
     
    Last edited: Jan 31, 2019
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi, why wouldn't you edit your post to use code tags?

    Anyway, I'll take a guess without reading the code.

    As you are using RigidBodies and Physics, there simply isn't magical down force, that would keep your character grounded.

    You move fast enough sideways, and it will be larger displacement sideways than what gravity will have in vertical direction, so you end up floating in mid air for a while. Then this repeats, as you go down hill.
     
  3. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    Unity built-in rigidbody physics aren't quite easy to use when it comes to this type of behaviour (In fact, it's a total pain let's admit it)

    Check this tutorial, it helped me a lot. (there is multiple episodes)
     
  4. ZachAR3

    ZachAR3

    Joined:
    Jan 27, 2019
    Posts:
    5
    @eses done i didnt even know about them new to the forums thanks!