Search Unity

trouble with boxcolliders and gravity

Discussion in 'Getting Started' started by PoppaP, May 9, 2020.

  1. PoppaP

    PoppaP

    Joined:
    May 1, 2020
    Posts:
    1
    hi, I recently started using unity and finally found a tutorial on how to create a move and jump script it works great but the only problem is that if you hold D while touching a vertical surface gravity will slow to a halt , Any help would be appreciated!





    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class playermv : MonoBehaviour
    4. {
    5.     public float speed;
    6.     public float jump;
    7.  
    8.     private float move;
    9.     private Rigidbody2D rb;
    10.     private bool isJumping;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody2D>();
    16.  
    17.  
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         move = Input.GetAxisRaw("Horizontal");
    24.  
    25.         rb.velocity = new Vector2(move * speed, rb.velocity.y);
    26.         if (Input.GetButtonDown("Jump") && !isJumping)
    27.         {
    28.             rb.AddForce(new Vector2(rb.velocity.x, jump));
    29.             isJumping = true;
    30.  
    31.         }
    32.     }
    33.  
    34.     void OnCollisionEnter2D(Collision2D other)
    35.     {
    36.         if (other.gameObject.CompareTag("Ground"))
    37.         {   Debug.Log("isJumping");
    38.             isJumping = false;
    39.         }
    40.  
    41.     }
    42.  
    43. }
    44.  
    45.