Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

2D Character won't jump. No Errors.

Discussion in 'Scripting' started by JranEQ, Apr 22, 2021.

  1. JranEQ

    JranEQ

    Joined:
    Jan 23, 2019
    Posts:
    1
    I can't figure this out. My character just won't jump. I'm not getting any errors.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class PlayerMovement : MonoBehaviour
    {
    public float movepseed;
    public float jumpForce;
    private Rigidbody2D rb;
    private bool facingRight = true;
    private float moveDirection;
    private bool isJumping = false;
    // Awake is called after all objects are initiated. Called in render order
    private void Awake()
    {
    rb = GetComponent<Rigidbody2D>(); //will look for a component on this GameObject (what is attached to to) of type rigidbody2D
    }

    // Update is called once per frame
    void Update()
    {
    // get player input
    ProcessInputs();
    //animate
    Animate();
    }
    private void FixedUpdate()
    {
    //move
    Move();
    }
    private void Move()
    {
    rb.velocity = new Vector2(moveDirection * movepseed, rb.velocity.y);
    if(isJumping)
    {
    rb.AddForce(new Vector2(0f, jumpForce));
    }
    isJumping = false;
    }
    private void Animate()
    {
    if (moveDirection > 0 && !facingRight)
    {
    flipCharacter();
    }
    else if (moveDirection < 0 && facingRight)
    {
    flipCharacter();
    }
    }
    private void ProcessInputs()
    {
    moveDirection = Input.GetAxis("Horizontal");
    if(Input.GetButtonDown("Jump"))
    {
    isJumping = true;
    }
    }
    private void flipCharacter()
    {
    facingRight = !facingRight; //inversion bool
    transform.Rotate(0f, 180f, 0f);
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    14,332
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    If you are running a mobile device you can also see the console output. Google for how.
     
    Yoreki likes this.
  3. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    404
    Your code works for me.

    After adding the script to your Player, did you set the Jump Force in the Inspector?
     
    Yoreki likes this.
unityunity