Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Jirogirg · Mar 07, 2016 at 02:15 PM · movementcharactercontrollerupdatejumpfixedupdate

Problem with Jump script

Hi there. I'm trying to implement power jump - the longer you hold the button - the higher you will go. But, because of the fact than FixedUpdate called differs amounts in time per frame - my jumps height differs from time to time. How i can fix it?

     public float speed = 6.0f;
     public float jumpSpeed = 8.0f;
     public float gravity = 20.0f;
     public float rotateSpeedGrounded = 3.0f; // Rotation speed at ground
 
     public float inAirSpeed = 2.0f; // Adds or substracts to the movementspeed in air
     public float inAirDrift = 0.5f; // Adds Sidewards drift in air
     public float inAirRotation = 0.5f; // Rotation speed in air. Uncomment this to let the player change the face direction in air
 
     public float extraJumpForce = 5.0f;         //How much extra force to give to our jump when the button is held down
     public float maxExtraJumpTime = 2.0f;       //Maximum amount of time the jump button can be held down for
     public float delayToExtraJumpForce = 2.0f;  //Delay in how long before the extra force is added
     private float jumpTimer;             //Used in calculating the extra jump delay
     private bool playerJumped = false;           //Tell us if the player has jumped
     private bool playerJumping = false;          //Tell us if the player is holding down the jump button
 
     private Vector3 moveDirection = Vector3.zero;
     private float inputModifyFactor = 1.0f;
     private bool isGrounded     = false;
     public Transform groundChecker;
     private bool pressedJump     = false;
     private bool unpressedJump     = false;
     private bool pressingJump     = false;
 
     void Update() {
         pressedJump     = Input.GetButtonDown ("Jump");
         unpressedJump   = Input.GetButtonUp ("Jump");
     }
 
     void FixedUpdate() { 
 
         isGrounded = Physics.Linecast (transform.position, groundChecker.position);
         //float rayDistance = controller.bounds.extents.y;
         //isGrounded = Physics.Raycast (transform.position, Vector3.down, rayDistance + 0.085f);
 
         //If our player hit the jump key, then it's true that we jumped!
         if (pressedJump) {
             if (isGrounded) {
                 pressedJump = false;
                 playerJumped = true;   //Our player jumped!
                 playerJumping = true;  //Our player is jumping!
                 jumpTimer = Time.time; //Set the time at which we jumped
             }
         }
 
         //If our player lets go of the Jump button OR if our jump was held down to the maximum amount...
         if (unpressedJump || Time.time - jumpTimer > maxExtraJumpTime) {
             unpressedJump = false;
             playerJumping = false; //... then set PlayerJumping to false
         }
 
         CharacterController controller = GetComponent<CharacterController> ();
         //            // ---------------------------------------- At Ground -----------------------------------------
         if (isGrounded) {
             moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
             moveDirection *= speed;
             inputModifyFactor = (moveDirection.x != 0.0f && moveDirection.z != 0.0f) ? 0.7071f : 1.0f;
             moveDirection *= inputModifyFactor;
 
 
             if (playerJumped) {
                 moveDirection.y = jumpSpeed;
                 playerJumped = false;
             }
         }
         // ---------------------------------------------------- In Air  ----------------------------
         else {
             // We are not grounded. We can still influence the movement with the horizontal and vertical axis, but not so strong.
             moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), moveDirection.y, Input.GetAxis ("Vertical"));
             moveDirection.x *= speed;
             moveDirection.z *= speed;
             moveDirection.y -= gravity * Time.deltaTime;
         }
         // ---------------------------------------------------------------------------------------------        
         //Rotate
         Vector3 curDir = new Vector3 (moveDirection.x, 0, moveDirection.z);
         Vector3 newDir = Vector3.RotateTowards (transform.forward, curDir, (isGrounded ? rotateSpeedGrounded : inAirRotation) * Time.deltaTime, 0.0F);
         Debug.DrawRay (transform.position, newDir * 10, Color.red);
         transform.rotation = Quaternion.LookRotation (newDir);
 
         //Move the controller
         if (moveDirection != Vector3.zero) {
 
             if (playerJumping && Time.time - jumpTimer > delayToExtraJumpForce){
                 moveDirection.y = moveDirection.y + extraJumpForce;
                 //playerJumping = false;
             }
 
             controller.Move (moveDirection * Time.deltaTime);
         }
     }
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Jirogirg · Mar 08, 2016 at 02:47 PM

Thanks everyone for participation again!

Here's working code

 public class New_Player_Controller : MonoBehaviour {
     public float speed = 6.0f;
     public float jumpSpeed = 8.0f;
     public float gravity = 20.0f;
     public float rotateSpeedGrounded = 3.0f; // Rotation speed at ground
 
     public float inAirSpeed = 2.0f; // Adds or substracts to the movementspeed in air
     public float inAirDrift = 0.5f; // Adds Sidewards drift in air
     public float inAirRotation = 0.5f; // Rotation speed in air. Uncomment this to let the player change the face direction in air
 
     public float extraJumpForce = 5.0f;         //How much extra force to give to our jump when the button is held down
     public float maxExtraJumpTime = 2.0f;       //Maximum amount of time the jump button can be held down for
     public float delayToExtraJumpForce = 2.0f;  //Delay in how long before the extra force is added
     private float jumpTimer;             //Used in calculating the extra jump delay
     private bool playerJumped = false;           //Tell us if the player has jumped
     private bool playerJumping = false;          //Tell us if the player is holding down the jump button
 
     private Vector3 moveDirection = Vector3.zero;
     private float inputModifyFactor = 1.0f;
     private bool isGrounded     = false;
     public Transform groundChecker;
     private bool pressedJump     = false;
     private bool unPressedJump     = false;
     public float maxJump = 10;
     private CharacterController controller = new CharacterController();
 
     void Start() {
 
         controller = GetComponent<CharacterController> ();
 
     }
 
     void Update() {
     
         if (!pressedJump) {
             pressedJump = Input.GetButtonDown ("Jump");
         }
 
         if (!unPressedJump) {
             unPressedJump = Input.GetButtonUp ("Jump");
         }
     }
 
     void FixedUpdate() { 
 
         isGrounded = Physics.Linecast (transform.position, groundChecker.position);
         //float rayDistance = controller.bounds.extents.y;
         //isGrounded = Physics.Raycast (transform.position, Vector3.down, rayDistance + 0.085f);
 
         //If our player hit the jump key, then it's true that we jumped!
         if (pressedJump) {
             if (isGrounded) {
                 
                 pressedJump = false;
                 playerJumped = true;   //Our player jumped!
                 playerJumping = true;  //Our player is jumping!
                 jumpTimer = Time.time; //Set the time at which we jumped
             }
         }
 
         //If our player lets go of the Jump button OR if our jump was held down to the maximum amount...
         if (unPressedJump || Time.time - jumpTimer > maxExtraJumpTime) {
             playerJumping = false; //... then set PlayerJumping to false
             unPressedJump = false;
         }
 
         //CharacterController controller = GetComponent<CharacterController> ();
         //            // ---------------------------------------- At Ground -----------------------------------------
         if (isGrounded) {
             moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
             moveDirection *= speed;
             inputModifyFactor = (moveDirection.x != 0.0f && moveDirection.z != 0.0f) ? 0.7071f : 1.0f;
             moveDirection *= inputModifyFactor;
 
 
             if (playerJumped) {
                 moveDirection.y = jumpSpeed;
                 playerJumped = false;
             }
         }
         // ---------------------------------------------------- In Air  ----------------------------
         else {
             // We are not grounded. We can still influence the movement with the horizontal and vertical axis, but not so strong.
             moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), moveDirection.y, Input.GetAxis ("Vertical"));
             moveDirection.x *= speed;
             moveDirection.z *= speed;
             moveDirection.y -= gravity * Time.deltaTime;
 
             if (moveDirection.y > maxJump) {
 
                 moveDirection.y = maxJump;
                 playerJumping = false;
 
             }
         }
         // ---------------------------------------------------------------------------------------------        
         //Rotate
         Vector3 curDir = new Vector3 (moveDirection.x, 0, moveDirection.z);
         Vector3 newDir = Vector3.RotateTowards (transform.forward, curDir, (isGrounded ? rotateSpeedGrounded : inAirRotation) * Time.deltaTime, 0.0F);
         Debug.DrawRay (transform.position, newDir * 10, Color.red);
         transform.rotation = Quaternion.LookRotation (newDir);
 
         //Move the controller
         if (moveDirection != Vector3.zero) {
             if (playerJumping && Time.time - jumpTimer > delayToExtraJumpForce){
                 moveDirection.y = moveDirection.y + extraJumpForce;
             }
 
             controller.Move (moveDirection * Time.deltaTime);
         }
 
     }
         
 }



Comment
Add comment · Share
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

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

62 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

Related Questions

Jump logic issues 0 Answers

Grounded and pressing "jump" not working? 1 Answer

Using Velocity breaks my jumping 0 Answers

Double Jump using character controller 0 Answers

Frustrating Problem with Character Controller in FPS 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