Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Lolstarrr · Oct 13, 2021 at 02:31 AM · charactercontrollerjumpingdeltatime

How to make consistent jumping with CharacterController

I have a jumping system that jumps for a few seconds (or until key is released), but everytime I run the game the maximum height changes, as well as the jumping speed. Sometimes the player barely jumps, sometimes it jumps a lot and sometimes it jumps how it should. Here's the full code:

 public class player : MonoBehaviour
 {
     [SerializeField] private float speed;
      Vector3 moveVector;
      public CharacterController controller;
      float jumpHeight = 10.0f;
      float gravityValue = -9.81f;
      public float jumpTime;                 //how long has player jumped
      public float jumpTimer = 0.45f;  //how long to jump
      public float jumpSpeed = 0.8f;    //how fast should player jump
      public float gravity = 55.0f; 
     bool right = true;
     public bool running = false;
     public bool falling;
     public bool jumping;
     public bool canRun;
 
     public Animator animator;
     enum ButtonState{Released,Held};
      ButtonState previousButtonState;
 
      public bool CanJump
      {
     get
     {
         return controller.isGrounded && previousButtonState == ButtonState.Released;
     }
      }
      public bool CanContinueJump
      {
     get
     {
         return jumpTime <= jumpTimer && controller.isGrounded == false;
     }
      }
      
 
     void Start()
      {
          controller = GetComponent<CharacterController>();
      }
 
     void Update()
     {
     //REeset the MoveVector
          moveVector = Vector3.zero;
  
     //jump system     
     if(controller.isGrounded == true) jumpTime=0f;
      bool jumpButtonPressed = Input.GetKey(KeyCode.Z);    
     if(jumpButtonPressed )
         {
         if (CanJump || CanContinueJump)
         {
             moveVector.y = jumpSpeed/3;
             jumpTime += Time.fixedDeltaTime;
         }
         }
  
          moveVector.y = moveVector.y - (gravity * Time.fixedDeltaTime);
          controller.Move(moveVector * Time.deltaTime);
 
 // right and left movement
          if(Input.GetKey(KeyCode.RightArrow) ) Move(speed);
          if(Input.GetKey(KeyCode.LeftArrow) ) Move(-speed);
 
 //For animator when running
      if( (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow)) && canRun==true )
         running = true;
      if( (Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow))  )
         running = false;
 
 //for animator when jumping
      if( Input.GetKey(KeyCode.Z) && CanContinueJump )
      {
         jumping = true;
         canRun = false;
      } else
      if( Input.GetKey(KeyCode.Z) && controller.isGrounded == false)
       {
         jumping = false;
         falling = true;
      } else if( controller.isGrounded == false)
      {
         jumping = false;
         falling = true;
         canRun = false;
      } else
      {
         jumping = false;
         falling = false;
         canRun = true;
      }
 
     animator.SetBool("running", running);
     animator.SetBool("jumping", jumping);
     animator.SetBool("falling", falling);
 
     previousButtonState = jumpButtonPressed ? ButtonState.Held : ButtonState.Released;
          
     
     }
 
     void Move(float velocity) //move left or right
     {
     if(velocity <0 && right == true)
     {
         transform.Rotate(new Vector3(0.0f, 180.0f, 0.0f) );
         right = false;
     }
     if(velocity > 0 && right == false)
     {
         transform.Rotate(new Vector3(0.0f, 180.0f, 0.0f) );
         right = true;
     }
     transform.position += Vector3.right * velocity * Time.deltaTime;
     }
 }

I tried using deltaTime and fixedDeltaTime, but both have the same issue with jumping.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

185 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hold the jump button to jump higher with CharacterController 1 Answer

Odd behavior using CharacterController.isGrounded for jumping 1 Answer

How to move a character in one direction only when jumping? 1 Answer

I have a problem in the jump system. 1 Answer

Jump logic issues 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges