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

Character jump issue?

Discussion in 'Scripting' started by BSmith156, Feb 25, 2015.

  1. BSmith156

    BSmith156

    Joined:
    Feb 25, 2015
    Posts:
    11
    Hello, I'v been coding in C# for about 2 weeks, I'm planning on making a game where the player needs to adapt to challenges there faced with (No jump, no gun, etc) but obviously for this I need my character to jump, I'v been trying to figure out why my code isn't working for ages but I just can't figure it out, can anyone please tell me what I'm doing wrong? (And I know the code is simple)

    using UnityEngine;
    using System.Collections;

    public class PlayerScript : MonoBehaviour {

    public float moveSpeed = 5;
    public float rotateSpeed = 180;
    public float jumpSpeed = 20;
    public float gravity = 9.8f;
    CharacterController controller;
    Vector3 currentMovement;

    void Start ()
    {
    controller = GetComponent<CharacterController> ();
    }

    void Update ()
    {
    transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed * Time.deltaTime, 0);
    currentMovement = (new Vector3 (0, currentMovement.y, Input.GetAxis ("Vertical") * moveSpeed));
    currentMovement = transform.rotation * currentMovement;

    if (!controller.isGrounded)
    currentMovement -= new Vector3(0, gravity * Time.deltaTime, 0);
    else
    currentMovement.y = 0;

    if (controller.isGrounded && Input.GetButtonDown ("Jump"))
    currentMovement.y = jumpSpeed;

    controller.Move (currentMovement * Time.deltaTime);
    }
    }

    If anyone can help me please do. :)
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  3. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Also, explain what isn't working. Are you hitting the jump button and nothing happens? Is your character stuttering but not leaving the ground?

    I would remove the gravity check for now and take things one step at a time. Just make it so that pressing jump propels the character up. Once that is working, add in gravity to bring him back down.
     
  4. BSmith156

    BSmith156

    Joined:
    Feb 25, 2015
    Posts:
    11
    My problem is my character won't jump when I press the jump, I also used Debug.log to test if the if statement (if (controller.isGrounded && Input.GetButtonDown ("Jump"))) is working and it is, I will try getting rid of the gravity code though.

    Edit: Nope, it didn't work, I tried removing the gravity code and just setting the variable to 0 but none worked. Thanks for trying though :)
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Your code:
    Code (CSharp):
    1. if (!controller.isGrounded)
    2. currentMovement -= new Vector3(0, gravity * Time.deltaTime, 0);
    3. else
    4. currentMovement.y = 0;
    5.  
    6. if (controller.isGrounded && Input.GetButtonDown ("Jump"))
    7. currentMovement.y = jumpSpeed;

    As soon as you hit the button, you'll take off for one (or a few) frames until 'isGrounded' will return false, then it'll run into the first if again and your jumpSpeed will no longer apply.

    *edit nvm, didn't see the 'minus', my bad
     
    Last edited: Feb 25, 2015
  6. BSmith156

    BSmith156

    Joined:
    Feb 25, 2015
    Posts:
    11
    I tried removing the 'isGrounded' check from the if statement but it doesn't make a difference, I also removed the whole gravity code (!controller.isGrounded) but it didn't do anything either.
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Sorry, forget what i just wrote. I just saw there is a minus so that should work properly. -_-
    I'll try your code in a second.
     
  8. BSmith156

    BSmith156

    Joined:
    Feb 25, 2015
    Posts:
    11
    Ok, thanks. I seriously am starting to think it isn't to do with my code but with untiy. I guess we'll find out if it works for you.
     
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Well, okay. It actually works, but not always.

    isGrounded toggles all the time, you may want to add a small gravity even though you're grounded to be sure the ground collision is detected appropriately.
     
  10. XenoJester

    XenoJester

    Joined:
    Feb 25, 2015
    Posts:
    13
    If your if statement is firing correctly,
    then the only problem would be with
    Code (csharp):
    1. currentMovement.y = jumpSpeed;|
    Does jump speed = 0 by accident?
     
  11. BSmith156

    BSmith156

    Joined:
    Feb 25, 2015
    Posts:
    11
    Ok.Thanks
     
  12. nygren

    nygren

    Joined:
    Feb 13, 2014
    Posts:
    33
    Check your Character Controller's Min Move Distance.

    Since you only add movement to y in one frame (GetButtonDown() only returns true one frame): if you have a decent FPS you'll get a low y movement (Time.deltaTime will be a low value) and so Min Move Distance might prevent your move.
     
  13. XenoJester

    XenoJester

    Joined:
    Feb 25, 2015
    Posts:
    13
    That's a good point, could try GetButton instead of just GetButtonDown, so it fires multiple frames while you hold space.
     
  14. BSmith156

    BSmith156

    Joined:
    Feb 25, 2015
    Posts:
    11
    I'v been experimenting and I found when I put my character in the floor (Without gravity) and pressed space it jumped, so I think it's something to do with the 'isGrounded' detection. When I put gravity on and looked closely I could see a small space between my character and the floor, any way to fix this?

    Edit: I tried the remove graviy and put character in floor thing again and it didn't work, I'm just plain confused now.
     
  15. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    That might be caused by the skin width of the CharacterController, but you shouldn't make it too thin afaik.
    Anyway, does it still not jump at all or just sometimes?
     
  16. BSmith156

    BSmith156

    Joined:
    Feb 25, 2015
    Posts:
    11
    It still doesn't jump at all, the one time where it did jump (When there was no gravity and it was in the floor) it only jumped sometimes.
     
  17. BSmith156

    BSmith156

    Joined:
    Feb 25, 2015
    Posts:
    11
    I changed the height of the collision box to 1.9 instead of 2 which seems to have fixed the problem of it not jumping at all and I can just model my character around that collision box, however it doesn't always jump.
     
  18. XenoJester

    XenoJester

    Joined:
    Feb 25, 2015
    Posts:
    13
    Well sounds like the isGrounded detection isn't working properly, personally I never use the controller.isGrounded because I found it to be finicky. I reccommend shooting a ray out of the bottom like this and seeing if the dist is within a certain tolerance.

    groundDist can just be a public float you set in the inspector.

    Code (csharp):
    1.  
    2. void checkGround()
    3. {
    4.      RaycastHit hit;
    5.  
    6.      if(Physics.Raycast (transform.position, Vector3.down,out hit, Mathf.Infinity))
    7.      {
    8.        if(hit.distance < groundDist)
    9.        {
    10.          grounded = true;
    11.        }
    12.        else
    13.        {
    14.         grounded = false;
    15.        }
    16.      }
    17. }
    18.  
     
  19. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Have you then tried to apply a small gravity instead of 0 in your 'else'?
     
  20. BSmith156

    BSmith156

    Joined:
    Feb 25, 2015
    Posts:
    11
    Thanks everyone for your help, my code works now :)

    Here's the code in case anyone has the same problem or is interested:

    void Update ()
    {
    transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed * Time.deltaTime, 0);
    currentMovement = new Vector3 (0, currentMovement.y, Input.GetAxis ("Vertical") * moveSpeed);
    currentMovement = transform.rotation * currentMovement;

    if (!controller.isGrounded)
    currentMovement -= new Vector3(0, gravity * Time.deltaTime, 0);
    else
    currentMovement.y = -0.5f;

    if (controller.isGrounded && Input.GetButtonDown("Jump"))
    currentMovement.y = jumpSpeed;

    controller.Move (currentMovement * Time.deltaTime);
    }
    }
     
  21. dreamagery2

    dreamagery2

    Joined:
    Jan 31, 2015
    Posts:
    1
    I'm having a similar problem. I want to avoid using physics and make a jump that will be realistic using the following code. But I can't find the problem here
    • Also, I'm trying to have the player move forward/back, turn, and do a single jump with a realistic arc
    :
     

    Attached Files:

    Last edited: Feb 28, 2015
  22. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    First of all, please use Code Tags instead of posting files.
    Secondly, please create a new thread unless it's something which contributes to the actual topic/issue.

    Your code looks a little inconsistent as to how you calculate the values unless that's your intention.
    E.g. your forward velocity is just set to your raw speed, backward velocity is speed*Time.deltaTime, your rotation is done without Time.deltaTime and the jumping with Time.deltaTime again.
    You should be doing it the same way all the time, otherwise it's really confusing.

    One common way is to calculate the abosulte movement Vector first and multiply it by Time.deltaTime when everything else has been done.

    Also, your backward velocity will be a forward velocity due to minus&minus which equals plus.

    The code for jumping will probably not have any great effect as you only add the up velocity in a single frame (when the key is pressed down) while you decrease it by a much larger value all the time.
    There's already a basic and working jump speed calculation in the scripts above, you may have a look at it.

    Last but not least the argument in the controller.Move(...) looks a little strange, it's basically 2* one of the summands.
     
  23. harshalp2120

    harshalp2120

    Joined:
    Dec 17, 2021
    Posts:
    1
    Setting Min Move Distance of character controller to 0 fixed the issue for me