Search Unity

My jump script wont work.

Discussion in '2D' started by Dino730_, Jan 4, 2021.

  1. Dino730_

    Dino730_

    Joined:
    Jan 3, 2021
    Posts:
    3
    I'm pretty new to unity so I usually just follow tutorials to from YouTubers. This script is supposed to tell unity that when I "Jump" (press space) It makes me jump but when I go in play mode nothing happens. Here is my script. Another problem is that since I'm so new I don't know where on the internet to look for help so I hope this works and one of you Unity pros can fix this for me.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour{
    6.  
    7.     public CharacterController2D controller;
    8.  
    9.     public float runSpeed = 40f;
    10.  
    11.     float horizontalMove = 0f;
    12.     bool jump = false;
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
    18.  
    19.         if (Input.GetButtonDown("Jump"))
    20.         {
    21.             jump = true;
    22.         }
    23.     }
    24.  
    25.     void FixedUpdate () //FixedUpdate happens a specific time every second
    26.     {
    27.         controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
    28.         jump = false;
    29.     }
    30. }
    31.  
     
    Last edited: Jan 6, 2021
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,463
    1st: You should post your code in code tags (see the ribbon before you post, or edit your post and change it).
    2nd: You are setting the jump bool to true when you press space, but you are making jump = false every time FixedUpdate runs (a lot).
    3rd: You havent defined what happens when jump = true. Once it becomes true something should happen.

    Basically, you will want to have a rigidbody2d on your object, when jump = true, a force is applied one time upwards on the character. Also, you want some way to check if the player is grounded. When the player is grounded, they can jump, but when they are not (aka in the air) then grounded = false and they cannot jump.

    Check out this video on movement and jumping.

     
    eses likes this.
  3. Dino730_

    Dino730_

    Joined:
    Jan 3, 2021
    Posts:
    3
    That is the video that I tried using :/ and I followed all the steps
    also I tried doing what you said about adding a force whenever I jump but I couldn't figure out how to. I also already have a rigid body 2D on my player
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @SuperDino730

    Make sure you have that controller part added to your character, which handles movement.

    Also watch the whole video. IIRC Brackeys rarely had anything major wrong in his video edits (= scripts usually work without editing).
     
  5. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,463
    Definitely follow what @eses suggested. As for adding force, use the rigidbody2D.AddForce method.

    https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html

    Something like this:
    rb2D.AddForce(transform.up * thrust);

    Since you are so new, a word of advice (and i mean this in the most helpful way possible) is to get really good at Googling your issues. Im sure you have tried googling this but try to break it down into pieces.

    For example, you are having jumping issue and you are using a rigidbody2D, so look up Rigidbody2D on the docs and scroll down until you see the methods. Those are the methods that will appear when you type your rigidbody reference.
    https://docs.unity3d.com/ScriptReference/Rigidbody2D.html

    Look for the AddForce and then click on that. If it still doesnt work, google "Rigidybody2D.AddForce not working".

    Another helpful thing is to use Debug.Logs liberally. The are a great way to easily test if parts of your code is running. Throw one in Start to make sure the script is even running, throw one in your Jump function (when you press space) to make sure that it is registering the input, etc. I use these all the time, even after like 3 years of working with Unity and I was as new as you are to Unity and programming.
     
    eses likes this.
  6. Dino730_

    Dino730_

    Joined:
    Jan 3, 2021
    Posts:
    3
    @Cornysam I tried using add force and watched the whole video but nothing seems to work. I also tried searching RigidBody2D.Adforce not working but still didn't find anything. In the Brackeys video he gave a link to the character controller so I used that, maybe their is something wrong with the code or just a bug with unity. I still have no idea what is wrong though. EDIT: I also tried Debug.Log and it was picking up the jump but just not executing it
     
  7. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,463
    Try increasing the jumpForce a lot. Depending on the character's mass and the gravity, even a value of like 50 may not show anything. Make it like 150 or more to see something.
     
  8. Sadanjane

    Sadanjane

    Joined:
    Jul 1, 2020
    Posts:
    3
    Hi @SuperDino730 , I'm a beginner in unity, But my code works here. Try this one

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private float speed = 5.0f;
    9.     [SerializeField]
    10.     private float jumpForce = 5.0f;
    11.  
    12.     private string GROUND_TAG = "Ground";
    13.     private string RUN_ANIMATION = "run";
    14.  
    15.     private SpriteRenderer sr;
    16.     private Animator anim;
    17.     private Rigidbody2D rigidbody;
    18.  
    19.     public bool isGround = true;
    20.  
    21.     private float moveX;
    22.  
    23.     private
    24.  
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.  
    30.         sr = GetComponent<SpriteRenderer>();
    31.         anim = GetComponent<Animator>();
    32.         rigidbody = GetComponent<Rigidbody2D>();
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         jumpPlayer();
    39.         AnimatePlayer();
    40.     }
    41.  
    42.     private void FixedUpdate()
    43.     {
    44.        
    45.         ControllerKeyboard();
    46.     }
    47.  
    48.     private void ControllerKeyboard()
    49.     {
    50.  
    51.         if (Input.GetKey(KeyCode.A) )
    52.         {
    53.             transform.position += Vector3.left * speed * Time.deltaTime;
    54.         }
    55.         else if (Input.GetKey(KeyCode.D))
    56.         {
    57.             transform.position += Vector3.right * speed * Time.deltaTime;
    58.         }
    59.     }
    60.  
    61.     void jumpPlayer()
    62.     {
    63.         if((Input.GetButtonUp("Jump")) && isGround)
    64.         {
    65.             rigidbody.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
    66.             isGround = false;
    67.         }
    68.     }
    69.  
    70.     private void OnCollisionEnter2D(Collision2D collision)
    71.     {
    72.         if (collision.gameObject.CompareTag(GROUND_TAG))
    73.         {
    74.             isGround = true;
    75.         }
    76.     }
    77.  
    78.     private void AnimatePlayer()
    79.     {
    80.         moveX = Input.GetAxisRaw("Horizontal");
    81.         if(moveX < 0)
    82.         {
    83.             sr.flipX = true;
    84.             anim.SetBool(RUN_ANIMATION, true);
    85.         }else if(moveX > 0)
    86.         {
    87.             sr.flipX = false;
    88.             anim.SetBool(RUN_ANIMATION, true);
    89.         }
    90.         else
    91.         {
    92.             anim.SetBool(RUN_ANIMATION, false);
    93.         }
    94.     }
    95. }
    96.  
     
  9. Sadanjane

    Sadanjane

    Joined:
    Jul 1, 2020
    Posts:
    3
    I think Brackey is using a third party characterController2D component.
     
  10. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    45
    I am having a similar problem, except that I am getting the error code that 'jump' is not defined, but I have it set up in my PlayerInput window: I have checked the script for the PlayerInput and Jump is clearly defined in it, does anyone have suggestions other than to google it, because to be honest this forum is a last resort after having taken google as far as I could.
    PlayerInput.PNG
     
  11. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,463
    You spelled jump with a lowercase j in your text above, but the screenshot shows "Jump" with a capital J. C# is case sensitive. If that doesnt work, share your code.
     
  12. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    45
    Jump appears in the code correctly, I was just stating an example not a direct quote of what is in the script. The issue was not in the code, it was in the Input system. Had to delete it and create a new one, now the script recognizes the Jump command, but for some reason the Mixamo animation doesn't work. I have the transitions set up like the tutorials say, but I can't figure out why the transition from idle, run, walk, any state, or just putting Jump in the animator by itself doesn't work. I am more than likely going to have to delete and re download the animation and convert it to humanoid again. Thanks for your reply. Do you happen to know how to set up a character's ability to pick up and throw an object using the input system? All the tutorials I find using google do not explicitly teach this.
     
  13. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,463
    Ive seen tutorials on picking up and throwing things but they seem to do them differently and with different results. I dont know how exactly but essentially you need a PickUp animation and a Throw animation. Then, when the player reaches a an object that can be picked up, it detects with with a raycast or collider it checks for the Input. Once input is called it "picks up the item" via animation and the code. Then you have some sort of bool or check that knows there is an item to throw and after some Input press you "throw" the object.
     
    kingdom216 likes this.
  14. Deleted User

    Deleted User

    Guest

    This might be a little late but, check if your Ground Check is right. I moved my Ground Check so that it was a little under the collision box and it worked.