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 Had1t1x · Jan 07, 2018 at 10:05 PM · movementfpsjumpingfps controllerfaster

Character sometimes moves faster when I jump and start moving at the same time

Hello there!

I have a very annoying bug that occurs when I jump and start moving at the same time that makes my fps character move faster than it should.

Here is the code attached to the player without the unnecessary things :

 void FixedUpdate () {
     // Update the isRunning variable only if the player is grounded
     if (controller.isGrounded) {
         isRunning = inputManager.sprintInput;
     }
 
     // Execute all the scripts that move and rotate the player
     ApplyGravity ();
     RotateFromCamera ();
     MoveFromInput ();
     Jump ();
 
     // Move
     controller.Move (velocity * Time.fixedDeltaTime);
     velocity = controller.velocity;
 }
 
 void ApplyGravity () {
     // Check for exit cases
     if (controller.isGrounded) {
         velocity.y = -stickToGroundForce;
         return;
     }
 
     // Change the velocity y
     velocity.y -= stats.gravity;
 }
 
 void Jump () {
     // Check if we are pressing the jump button
     if (inputManager.jumpInput) {
         // Set the jumpInput variable to false to prevent the player from jumping infinitly
         inputManager.jumpInput = false;
 
         // Check for exit cases
         if (!controller.isGrounded) {
             return;
         }
 
         // Set the velocity on the y axis to the jump force
         velocity.y = stats.jumpForce;
     }
 }
 
 void MoveFromInput () {
     #region Get the speeds
     // Control the speed on the x axis
     targetHSpeed = (isRunning) ? stats.sprintSpeed * inputManager.moveInput.x : stats.runSpeed * inputManager.moveInput.x;
     currentHSpeed = Mathf.SmoothDamp (currentHSpeed, targetHSpeed, ref hSpeedSmoothVelocity, GetInAirModifiedSmoothTime (speedSmoothTime));
 
     // Control the speed on the z axis
     targetVSpeed = (isRunning) ? stats.sprintSpeed * inputManager.moveInput.y : stats.runSpeed * inputManager.moveInput.y;
     currentVSpeed = Mathf.SmoothDamp (currentVSpeed, targetVSpeed, ref vSpeedSmoothVelocity, GetInAirModifiedSmoothTime (speedSmoothTime));
     #endregion
 
     // Get the direction to move so we can't change it while jumping or falling
     if (controller.isGrounded) {
         right = transform.right;
         forward = transform.forward;
     } else {
         right = (right != Vector3.zero) ? right : transform.right;
         forward = (forward != Vector3.zero) ? forward : transform.forward;
     }
 
     // Update the velocity
     velocity = currentHSpeed * right + Vector3.up * velocity.y + currentVSpeed * forward;
 }
 
 void RotateFromCamera () {
     transform.eulerAngles = new Vector3 (transform.eulerAngles.x, mainCam.eulerAngles.y, transform.eulerAngles.z);
 }
 
 float GetInAirModifiedSmoothTime (float smoothTime) {
     // Return the initial value if the player is grounded
     if (controller.isGrounded) {
         return smoothTime;
     }
 
     // Return the maximum value if the air control % = 0
     if (airControlPercent == 0f) {
         return float.MaxValue;
     }
 
     // Return the calculated smooth time if the exit cases are false
     return smoothTime / airControlPercent;
 }

The input manager updates the input variables on both update and fixed update calls and the camera rotates only on update calls

Comment
Add comment · Show 1
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
avatar image Had1t1x · Jan 20, 2018 at 05:46 PM 0
Share

Alright, so I am pretty sure this is appening because of the GetInAir$$anonymous$$odifiedSmoothTime () function: the current speed takes too much time to go back to zero and the jump is waaaaay too long. So right now I am looking for a solution; maybe I can just affect the input when I am not grounded ins$$anonymous$$d of affecting the smoothTime.

1 Reply

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

Answer by Had1t1x · Jan 20, 2018 at 06:46 PM

Yay! Bug fixed! So now I am affecting the input, as I said before. Basically I have a new lastGroundedInput variable that is equals to the inputHandler.lastMoveInput (equals to the moveInput recorded at each start of Update () calls) and that stays the same while not grounded. Now I have this function :

 Vector2 GetInAirModifiedMoveInput (Vector2 input, Vector2 lastGroundedInput) {
         // Return the initial value if the player is grounded
         if (isGrounded) {
             return input;
         }
     
         // Return the calculated input if we are grounded
         return lastGroundedInput * (1f - airControlPercent) + input * airControlPercent;
 }

You can see it like this: if the airControlPercent is equals to 90% (.9f), then you will have 10% (.1f) of the input that will stay the same during the jump/fall (so lastGroundedInput x ( 1f - airControlPercent)) and 90% of the input that will be equals to the current recorded input (input x airControlPercent).

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

183 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

Related Questions

Standard FPS Controller - Sounds Cancelling One Another 0 Answers

How Do I Get My Player To Move With The Block That Its On 1 Answer

Control Air movement with character controller 0 Answers

VR character rotation not follow the camera 1 Answer

Field ' ' is never assigned to and will always have its default value 0. Pls help me 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