Search Unity

Keep Jump Height

Discussion in '2D' started by PenguinLord, Nov 26, 2016.

  1. PenguinLord

    PenguinLord

    Joined:
    Sep 27, 2013
    Posts:
    9
    In my Script i'm using a jump Force, the reason being is because i don't know any other way of jumping.

    So sometimes when the Object Jumps it lowers its jump height when i move, any way of going around this?

    this is my code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class SimpleController : MonoBehaviour {
    4.  
    5. [HideInInspector] public bool jump = false;
    6.  
    7.  
    8. public float Speed = 0f;
    9. public Transform groundCheck;
    10. public float JumpForce;
    11.  
    12.  
    13. private float move = 0f;
    14. private bool grounded = false;
    15. private Rigidbody2D rb2d;
    16.     void Awake () {
    17.         rb2d = GetComponent<Rigidbody2D>();
    18.     }
    19.     void Update ()
    20.     {
    21.         grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
    22.  
    23.  
    24.         transform.eulerAngles=new Vector3(0,transform.eulerAngles.x,transform.eulerAngles.y); // Lock Rotations
    25.  
    26.         if(grounded)
    27.         {
    28.             jump = true;
    29.         }
    30.     }
    31.     void FixedUpdate () {
    32.         move = Input.GetAxis ("Horizontal");
    33.         GetComponent<Rigidbody2D>().velocity = new Vector2 (move * Speed, GetComponent<Rigidbody2D>().velocity.y);
    34.         if (jump)
    35.         {
    36.             rb2d.AddForce (new Vector2 (rb2d.velocity.x,JumpForce));
    37.             jump = false;
    38.         }
    39.     }
    40. }
     
  2. PenguinLord

    PenguinLord

    Joined:
    Sep 27, 2013
    Posts:
    9
    Trying to Recreate this type of jumping



    Skip to 4:10
     
  3. hiengs2702

    hiengs2702

    Joined:
    Nov 27, 2016
    Posts:
    5
    It's problem with addforce, the longer your Physic2D linecast check the layermask, the bigger jump force, when u move your player, time check will be shorter and the force is lower. u can jump by add the velocity directly to your player like :
    this.rigidbody.velocity=new Vector2();
     
  4. zioziozio

    zioziozio

    Joined:
    Sep 11, 2012
    Posts:
    10
    I would avoid using Physics for platforming. It's somewhat hard to get it to do what you want it to. But there is a great tutorial for a simple and effective platforming system here: https://www.youtube.com/playlist?list=PLFt_AvWsXl0f0hqURlhyIoAabKPgRsqjz

    With this tutorials, system you set the jump height and jump duration which is a bit more intuitive for designing a game.