Search Unity

How do you jump in 2D top down games?

Discussion in 'Scripting' started by Pixitales, Jun 17, 2019.

  1. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    How do I make my character transform translate up and down with a ground position and a goal position? No gravity and the sprite doesn't have any colliders or rigidbody2D attached to it.

    Code (CSharp):
    1.  
    2. public float jumpHeight;
    3. public bool isJumping;
    4. Vector3 groundPos;
    5. Vector3 goalPos;
    6.  
    7. void Update()
    8. {
    9.    if (Input.GetKeyDown("Space"))
    10.    {
    11.        StartCoroutine(Jumping());
    12.    }
    13. }
    14.  
    15. private IEnumerator Jumping()
    16. {
    17.     groundPos = transform.position
    18.  
    19.  
    20.     while(true)
    21.     {
    22.  
    23.     }
    24.  
    25. }
     
    Last edited: Jun 17, 2019
  2. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Most games accomplish this by moving the character slightly forwards & then backwards. You will want a shadow on the ground so to the user it looks like jumping.

    To actually jump over something you would want to disabled collision & move the player forwards to get over it.

    The main way to make it feel good & make sense to your users will br through the art. Just look at other 2d top down games with jumping to give you an idea.
     
  3. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    Yeah, i am trying to achieve something like that. The character has no collision, but the shadow underneath the character does. Not sure how to make this work because the childobject always lagging behind the parent when walking into a wall
     
  4. Deleted User

    Deleted User

    Guest

    Why didn't you add collision to the parent instead of the child?

    Or, what about not making the shadow the child of the character and putting the both of them into an empty game object that would have collision?
     
  5. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    Because i already tried that and with disabling and enabling the collider. Problem is you can jump over stuff or get stuck on a wall or jump timing is wrong.

    I havent tried your other idea yet. Sounds good. But who gets the player movement script?
     
  6. Deleted User

    Deleted User

    Guest

    I would give it to the parent but you may need to make some tests. :)
     
  7. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    Yeah or otherwise, i may really have to use fake 3D. I dont like working with sprites tilted in a 45 degree angle.