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. Dismiss Notice

[Help] Problem in making simple jumping character

Discussion in '2D' started by Steventus, Nov 7, 2016.

  1. Steventus

    Steventus

    Joined:
    Nov 6, 2016
    Posts:
    3
    Hi, I'm an absolute beginner and I want to get a grasp of Unity as a person with no background whatsoever in computing language.

    I'm following the 2D tutorial on
    https://unity3d.com/learn/tutorials/topics/2d-game-creation/2d-character-controllers?playlist=17093
    but I am not concerned with the animations therefore I have omitted the lines related to the animation however with the followingcode I have created, I cannot make a character jump.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.  
    7.     public float jumpforce = 10f;
    8.     public float maxSpeed = 10f;
    9.     private Rigidbody2D rb;
    10.  
    11.     bool grounded = false;
    12.     public Transform groundcheck;
    13.     public float groundRadius = 0.2f;
    14.     public LayerMask whatIsGround;
    15.  
    16.     // Use this for initialization
    17.     void Start()
    18.     {
    19.         rb = GetComponent<Rigidbody2D>();
    20.         CharacterController controller = GetComponent<CharacterController>();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         if (grounded && Input.GetKeyDown(KeyCode.Space))
    27.         {
    28.             rb.AddForce(new Vector2(0, jumpforce));
    29.         }
    30.     }
    31.  
    32.  
    33.     void FixedUpdate()
    34.     {
    35.         grounded = Physics2D.OverlapCircle(groundcheck.position, groundRadius, whatIsGround);
    36.  
    37.         float move = Input.GetAxis("Horizontal");
    38.  
    39.         rb.velocity = new Vector2(move * maxSpeed, rb.velocity.y);
    40.  
    41.     }
    42. }
    43.  
    Here is a reference screenshot of my work,
     
  2. Steventus

    Steventus

    Joined:
    Nov 6, 2016
    Posts:
    3
  3. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    You'll probably want to use ForceMode2D.Impulse, since you're modeling the jump as an instantaneous force (only forcing for one frame):

    rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    You never want to get Inputs in FixedUpdate, as FixedUpdate could be called more than once, or not at all between two game Updates depending on framerate.

    Get your Inputs in Update, then add your forces in FixedUpdate.
     
  5. Steventus

    Steventus

    Joined:
    Nov 6, 2016
    Posts:
    3
    Thanks a lot! Such a simple solution. I appreciate your help!

    I've took your points to great consideration. I'll be sure to change my code to reflect this. Thanks for the additional pointers~