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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[ask] 2D platformer movement and jump script

Discussion in '2D' started by Rnd--, Dec 18, 2014.

  1. Rnd--

    Rnd--

    Joined:
    Sep 27, 2012
    Posts:
    30
    Hi guys.. I need some help for scripting my 2D platformer
    Im using rigidbody2D, and set gravity scale into 1

    Im create walk script like this
    Code (CSharp):
    1.  
    2. if(Input.GetAxis("Horizontal") != 0)
    3.         {
    4.             velocityVector.x = Input.GetAxis("Horizontal") * moveSpeed;
    5.             velocityVector.y = transform.rigidbody2D.velocity.y;
    6.             transform.rigidbody2D.velocity = velocityVector;
    7.         }
    8.  
    and jump script like this
    Code (CSharp):
    1.  
    2. void jump()
    3.     {
    4.         if(!IsJumping)
    5.         {
    6.             _currentIncrement = 0;
    7.             rb.AddForce(Vector2.up * jumpPower);
    8.             IsJumping = true;
    9.             _isJumpCommandFinished = false;
    10.             StartCoroutine("jumping");
    11.         }
    12.     }
    13.  
    14.     IEnumerator jumping()
    15.     {
    16.         while (!_isJumpCommandFinished)
    17.         {
    18.             _currentIncrement += jumpIncrement;
    19.             var jumpFactor = _currentIncrement / (maxJump - jumpPower);
    20.             if (jumpFactor < 1f)
    21.                 rb.AddRelativeForce(Vector2.up * _currentIncrement, ForceMode2D.Impulse);
    22.             yield return null;
    23.         }
    24.         yield return null;
    25.     }
    26.  
    and heres the result
    https://dl.dropboxusercontent.com/u/60462866/test/publish.html

    but if you try jump and go to wall it will be stuck like this instead free fall with gravity
    jump.png

    Im debug the rb.velocity (rb = this.rigidbody2D)
    when jump the value is (0, 0..5) when jump, when fall (0, 0..-5)
    when walk the value is (0..4, 0). when jump and push right (fly to wall) somehow the value is (4, 0) it means gravity no effect in here. but when right button is release the value of y decreasing like free fall
    why the gravity has no effect when fly to wall and keep push right button ?? anyone have idea for this ??
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    right click in assets window - create - physics2D material. Then set Friction to 0. Change material of walls collider to this.
     
  3. Rnd--

    Rnd--

    Joined:
    Sep 27, 2012
    Posts:
    30
    :eek: wow thx man its really help