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

2D Jumping... Snaps to jump height and slowly falls

Discussion in 'Physics' started by ajlott, Jan 9, 2020.

  1. ajlott

    ajlott

    Joined:
    Jan 26, 2014
    Posts:
    14
    I am quite perplexed right now. Last time I did a 2D project this worked perfectly. Can anyone help me out? My issue should be evident from the thread title. I've provided a video, a screenshot of the rigidbody settings for the blue player object, and the relevant code. Thanks for any help!

    Code (csharp):
    1.  
    2. public class PlayerController : MonoBehaviour
    3. {
    4.     public float speed;
    5.     public float xForce;
    6.     public float yForce;
    7.  
    8.     // Update is called once per frame
    9.     void Update()
    10.     {
    11.         float move = Input.GetAxis("Horizontal");
    12.         GetComponent<Rigidbody2D>().velocity = new Vector2(move * speed, 0);
    13.     }
    14.  
    15.     private void FixedUpdate()
    16.     {
    17.         if (Input.GetKeyDown(KeyCode.Space))
    18.         {
    19.             GetComponent<Rigidbody2D>().AddForce(new Vector2(xForce, yForce));
    20.         }
    21.     }
    22. }
    23.  
     

    Attached Files:

  2. ajlott

    ajlott

    Joined:
    Jan 26, 2014
    Posts:
    14
    Figured it out. In the code where I move my player along the x axis, I am setting its y velocity to 0, so that when I try to add a vertical force or directly set its y velocity when jumping, it's constantly being reset to 0.