Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

player jumping weird

Discussion in 'Scripting' started by ghost123_1234, Oct 13, 2015.

  1. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
    hello

    i have this code
    Code (CSharp):
    1. private int multiPlier = 50;
    2.  
    3. private float jumpheight = 150;
    4. publicRigidbody rigidbodyonobject;
    5.  
    6.  
    7. void Start()
    8. {
    9. rigidbodyonobject = gameObject.GetComponent<Rigidbody>();
    10. }
    11.  
    12.  
    13.  
    14.  
    15. void Update()
    16.  
    17. {
    18.  
    19. if (Input.GetKeyDown(KeyCode.Space) && CheckGrounded() == true)
    20.  
    21.         {
    22.  
    23.             rigidbodyonobject.AddForce(gameObject.transform.up * Time.deltaTime * jumpheight * multiPlier);
    24.  
    25.             print("Jump");
    26.  
    27.         }
    28.  
    29.  
    30.  
    31.  
    32. }
    33. private bool CheckGrounded()
    34.     {
    35.         if (Physics.Raycast(transform.position, -gameObject.transform.up, out hit, 2f))
    36.         {
    37.             if (hit.collider.gameObject.tag == "floor")
    38.             {
    39.                 return true;
    40.             }
    41.             else
    42.             {
    43.                 return false;
    44.             }
    45.         }
    46.         return false;
    47.     }
    but then sometimes it jump high and sometimes it jump low?
    any suggestions so it goes a fixed height
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    GoofBall101 likes this.
  3. GoofBall101

    GoofBall101

    Joined:
    Jul 25, 2015
    Posts:
    57
  4. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    @GoofBall101 and @sander0711 - Just remember to use Time.fixedDeltaTime and NOT Time.deltaTime, otherwise you'll be multiplying the time between render frame with something that is using the physics update.

    In other words, it would speed up and slow down as the FPS goes up and down.

    And that is massive pain to figure out. Not that I would know about that though... :oops:
     
  5. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     private float speed = 5;
    7.     private int FastSpeed = 15;
    8.     public KeyCode EnableFastSpeedWithKey = KeyCode.LeftShift;
    9.  
    10.     public Transform cameraobject;
    11.     public Rigidbody rigidbodyonobject;
    12.    
    13.     private float jumpheight = 150;
    14.  
    15.     private RaycastHit hit;
    16.     private int multiPlier;
    17.  
    18.     // Use this for initialization
    19.     void Start()
    20.     {
    21.         rigidbodyonobject = gameObject.GetComponent<Rigidbody>();
    22.         multiPlier = 50;
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void FixedUpdate()
    27.     {
    28.         #region movement
    29.         if (Input.GetKey(KeyCode.C))
    30.         {
    31.             transform.localScale = new Vector3(1, 0.5f, 1);
    32.         }
    33.         else
    34.         {
    35.             transform.localScale = new Vector3(1, 1, 1);
    36.         }
    37.  
    38.         if (Input.GetKeyDown(KeyCode.Space) && CheckGrounded() == true)
    39.         {
    40.             rigidbodyonobject.AddForce(gameObject.transform.up * Time.fixedDeltaTime * jumpheight * multiPlier);
    41.             print("Jump");
    42.         }
    43.         if (Input.GetKey(EnableFastSpeedWithKey))
    44.         {
    45.             speed = FastSpeed;
    46.         }
    47.         else
    48.         {
    49.             speed = 5;
    50.         }
    51.         //VOORUIT
    52.         if (Input.GetKey(KeyCode.W))
    53.         {
    54.             Vector3 moveDirection = cameraobject.transform.forward;
    55.             moveDirection.y = 0f;
    56.             //  transform.Translate(moveDirection * Time.deltaTime * speed);
    57.             transform.localPosition += moveDirection * speed * Time.deltaTime;
    58.         }
    59.         //links
    60.         if (Input.GetKey(KeyCode.A))
    61.         {
    62.             Vector3 moveDirection2 = -cameraobject.transform.right;
    63.            moveDirection2.y = 0f;
    64.             transform.localPosition += moveDirection2 * speed * Time.deltaTime;
    65.         }
    66.         //rechts
    67.         if (Input.GetKey(KeyCode.D))
    68.         {
    69.             Vector3 moveDirection3 = cameraobject.transform.right;
    70.             moveDirection3.y = 0f;
    71.             transform.localPosition += moveDirection3 * speed * Time.deltaTime;
    72.         }
    73.         //acheruit
    74.        
    75.         if (Input.GetKey(KeyCode.S))
    76.         {
    77.             Vector3 moveDirection4 = -cameraobject.transform.forward;
    78.             moveDirection4.y = 0f;
    79.             transform.localPosition += moveDirection4 * speed * Time.deltaTime;
    80.         }
    81.  
    82.  
    83.        
    84.        
    85.         #endregion
    86.     }
    87.  
    88.     private bool CheckGrounded()
    89.     {
    90.         if (Physics.Raycast(transform.position, -gameObject.transform.up, out hit, 2f))
    91.         {
    92.             if (hit.collider.gameObject.tag == "floor")
    93.             {
    94.                 return true;
    95.             }
    96.             else
    97.             {
    98.                 return false;
    99.             }
    100.         }
    101.         return false;
    102.     }
    103. }
    so i did this so this is my code now: for the hole playercontrol. but it still jump weird sometimes it jump heigher then other times
     
  6. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,515
    "Just use FixedUpdate();" is the simple answer, but it's a bit trickier than that.

    You are testing Input.GetKeyDown() in FixedUpdate(). That is technically only valid for checking in Update(). For instance, if the key was NOT down, then it becomes down, Input.GetKeyDown() will only show true on the very single frame if you are looking at it in Update().

    If you are looking at it in FixedUpdate, which happens at a different framerate than Update, then that Input.GetKeyDown() may return true for more than one frame, triggering multiple applications of your force. It is even possible to completely miss the key going down event.

    What you want to do is test for the key going down in Update() and set a bool variable that says essentially, "Player has pressed jump."

    Then in FixedUpdate() you would test that bool variable, and when you apply the jump, clear the variable to false.

    If done properly, this will ensure a one-to-one mapping between keypresses and impulses added to your character.
     
  8. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
    so you mean something like this?
    Roughly not tested

    Code (CSharp):
    1. bool jump = false;
    2.  
    3. void Update()
    4. if(input.getkeydown(keycode.space)
    5. {
    6. jump = true
    7. }
    8. void fixedupdate()
    9. {
    10. if(jump == true && isgrounded == true)
    11. {
    12. //addforce for jump
    13. jump = false;
    14. }
    15. }
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,515
    @sander0711 that looks quick-eyeball correct, modulo API spelling. :)
     
  10. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
    yeah i know i did type it really quick :D
     
  11. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,546
    I think you would do good to check out a tutorial on character controllers.

    Here's a really good one by Sebastian Lague:


    Also, those variable names have got to go. Usually "camel backs" are the way to go. So instead of "jumpheight", try "jumpHeight" with the first word lower case and the first letter of the proceeding words capital.

    When you do it that way it will write it out correctly in the inspector as "Jump Height" if it's public.

    Also, also, make sure you're using names that aren't already used for something. "speed" is already used by Unity. Try "mySpeed" or something similar.

    Also, also, also you're multiplying with deltaTime, not fixedDeltaTime. The fixed update is done 50 times a second (by default), and the regular update is done as fast as possible. Right now you are telling Unity to apply the changes 50 times a second and multiplying it with 1/FPS.

    Also, also, also, also I noticed you've got a float for "speed" and an int for "FastSpeed". You can sometimes juggle the math between those two variables, but it's best practice to use one or the other. In this case, I would do a public float so you can tweak it in the editor.

    Honestly, I think you're better of doing a wipe and trying again after doing a few more tutorials. Looking at your code, it looks like you're trying to skip some learning steps.