Search Unity

Keeping the jump impulse when heading 2D

Discussion in '2D' started by zonetecde, Feb 14, 2020 at 9:29 PM.

  1. zonetecde

    zonetecde

    Joined:
    Friday
    Posts:
    1
    Good morning,

    I'm a beginner with Unity and to train I create a simple platform game. I have a character that can go left, right and jump. Here's the code :

    Code (CSharp):
    1. [SerializeField]
    2.      float speed = 0.1f;
    3.      [SerializeField]
    4.      float jumpHeight = 50f;
    5.      Rigidbody2D rb;
    6.      Transform myTransform;
    7.      bool canJump = false;
    8.      // Start is called before the first frame update
    9.      void Start()
    10.      {
    11.          myTransform = transform;
    12.          rb = this.GetComponent<Rigidbody2D>();
    13.      }
    14.      // Update is called once per frame
    15.      void Update()
    16.      {
    17.          // Player mouvement
    18.          if(Input.GetKey("d") || Input.GetKey("right")) // Droite
    19.          {
    20.              myTransform.position = new Vector3(myTransform.position.x + speed, this.transform.position.y);
    21.              myTransform.localRotation = Quaternion.Euler(0, 0, 0); // Retourne le sprite vers la droite
    22.          }
    23.          if (Input.GetKey("q") || Input.GetKey("left")) // Gauche
    24.          {
    25.              myTransform.position = new Vector3(myTransform.position.x - speed, this.transform.position.y);
    26.              myTransform.localRotation = Quaternion.Euler(0, 180, 0); // Retourne le sprite vers la gauche
    27.          }
    28.          if ((Input.GetKey("z") || Input.GetKey("up")) && canJump) // Jump si on est pas en l'aire et qu'on touche le sol
    29.          {
    30.              // Ne peux pas jump tant qu'aucune collision n'a été touché
    31.              canJump = false;
    32.              rb.AddForce(new Vector2(0, jumpHeight * 50));
    33.          }
    34.      }
    35.      /// <summary>
    36.      /// Informe que le sol est touché donc qu'on peut jump
    37.      /// </summary>
    38.      /// <param name="collision"></param>
    39.      private void OnCollisionEnter2D(Collision2D collision)
    40.      {
    41.          canJump = true;
    42.      }
    What I would like to make the game more difficult is that when you jump and go from right to left, the impulse doesn't change because, if I jump and go right, the impulse stops immediately which makes the game very easy.

    Do you have an idea? Thanks
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    1,737
    @zonetecde

    "What I would like to make the game more difficult is that when you jump and go from right to left, the impulse doesn't change because, if I jump and go right, the impulse stops immediately which makes the game very easy."

    Do you mean by this, you want to remove the air control? So when the character is in air, you cannot change the movement direction, and the character only follows its jump path?
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,245
    Do not change the Transform when using physics. You're using forces to get the body to move yet you're also bypassing the body by directly setting the Transform. Use the Rigidbody2D API to move anything to do with physics.
     
unityunity