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

Making 2D Physics Just Right

Discussion in '2D' started by HaydarLion, Dec 13, 2015.

  1. HaydarLion

    HaydarLion

    Joined:
    Sep 17, 2014
    Posts:
    62
    Hi all,
    I'm currently creating a 2D platformer game. The walking movement speed and jump height are what they should be however the gravity and physics seem a bit off. I don't want my character moving faster in the air than when walking and i dont want the character to slide when it lands after a jump (when it was moving in the air). What are the right settings for gravity and physics materials to make the game go smoothly, and if that's not what I should be adjusting what is?

    Thanks.
     
  2. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    I leave gravity and physics settings at their defaults. In terms of consistent walking speed and jumping speed I use the following variable weights:
    For walking, I use this within FixedUpdate()...
    Code (CSharp):
    1. if(!playerJumping){
    2.   rigidbody2D.velocity = new Vector2(0.8f, rigidbody2D.velocity.y);
    3. }
    When the player jumps I set playerJumping=true, and then I wait for a collision event which tells me that the player has ended the jumping routine and then I set playerJumping=false. Then that walking code is executed again (and prevents the player from sliding after landing the jump).
    By the way for jumping I use this...
    Code (CSharp):
    1. rigidbody2D.AddForce(new Vector2(100f,200f));
     
    HaydarLion likes this.
  3. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    Using the physics engine for platformers is pretty tricky. If you are going for very precise movement and input response (like older platformers) I would look into writing a custom character controller and use raycasting for collision checks.

    Here are a couple tutorials to give you an idea of what I mean :)

    http://deranged-hermit.blogspot.com/2014/01/2d-platformer-collision-detection-with.html

    http://www.gamasutra.com/blogs/Yoan...hobbyist_coder_1_2D_platformer_controller.php

    This is also very useful for polishing up the way your character controls..

    http://www.gamasutra.com/blogs/Yoan...w_to_avoid_limpness_and_rigidity_feelings.php
     
  4. originalterrox

    originalterrox

    Joined:
    Feb 6, 2015
    Posts:
    40
    I think gravity should be increased for platformers, realistic gravity is a bit slow.