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 /
avatar image
0
Question by ispeelgood · Aug 17, 2015 at 02:19 AM · movementfpsupdategravity

Jumping in my FPS causes the player to teleport instead of smoothly jumping, and gravity doesn't work as it's supposed to

Hello,

I am creating an FPS game in Unity 5.0.1f1 with C# that has Quake-like movement, and as such I have used this excellent resource to get my movement script going. I am not using the default FPS prefabs, but I'm using a Character Controller for my player.

This person had a similar problem to me, but I'm using the same calculations for my jump, so that question (and a lot of similar questions I found) didn't fix my errors.

I'm not going to quote the entire script (which is pretty lengthy) but I'll post a few snippets and explain the problems I have.

I run the following function at the start of Update() to know what the player has pressed.

 void UpdateInputs()
     {
         playerInput.moveForward = Input.GetAxis("Vertical");
         playerInput.moveRight = Input.GetAxis("Horizontal"); 
         playerInput.wishJump = Input.GetKey (KeyCode.Space);
     }

playerInput is an object of a class that I have created to have access to these values all over the script.

Now that I have my inputs in Update, I handle the rest in FixedUpdate.

This is my FixedUpdate(), which handles Ground Movement, Air Strafe, jumping and gravity.

     void FixedUpdate()
     {
         if (controller.isGrounded) {
 
             //reset vertical velocity and do a ground move
             new_velocity.y=0;
             new_velocity = MoveGround (PlayerAccelDirection (), prev_velocity);
 
             if(playerInput.wishJump)
             {
                 new_velocity.y+=jump_speed;
                 playerInput.wishJump = false;
                 Debug.Log ("NEW VELOCITY Y IS " + new_velocity.y);
             }
 
         }
         else
         {
             //do an air move and do gravity stuff
             new_velocity = MoveAir (PlayerAccelDirection (), prev_velocity);
             new_velocity.y -= gravity * Time.deltaTime;
         }
 
         controller.Move(new_velocity * Time.fixedDeltaTime);
     }

I've omitted the functions MoveGround, MoveAir, and Accelerate (found inside aformentioned functions) because they're 1-1 copies of the code in the first link I posted.

PlayerAccelDirection() is pretty simple and is as follows:

 private Vector3 PlayerAccelDirection()
     {
         Vector3 direction = new Vector3(playerInput.moveRight, 0, playerInput.moveForward);
         direction = transform.TransformDirection(direction);
         return direction;
     }

Now that I've posted a bunch of code and put it in context as best as I could, I will explain the problems I am currently facing.

  1. When I jump, my character suddenly teleports to the apex of his jump instead of jumping smoothly.

  2. When I fall from a height, my character falls with a constant velocity instead of accelerating downwards.

I'm thinking that I have either confused my deltaTimes or there's some logical error in there that I haven't noticed.

Thank you very much in advance for any help.

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
1
Best Answer

Answer by ispeelgood · Aug 19, 2015 at 09:59 PM

I ended up solving this problem myself.

I found out through thorough Debug Logging that for some reason my previous speed Y was not saved, so I added a variable to the start of my script:

 private float prev_y;

and adjusted my FixedUpdate like this:

 void FixedUpdate()
     {
         if (controller.isGrounded) {
             //reset vertical velocity and do a ground move
             prev_y=0.0f;
             new_velocity = MoveGround (PlayerAccelDirection (), prev_velocity);
 
             if(Input.GetKey (KeyCode.Space))
             {
                 prev_y+=jump_speed;
                 wishJump = false;
             }
 
         }
         else
         {
             //do an air move and do gravity stuff
             new_velocity = MoveAir (PlayerAccelDirection (), prev_velocity);
         }
 
         new_velocity.y = prev_y;
         new_velocity.y -= gravity * Time.deltaTime;
         prev_y = new_velocity.y;
         Debug.Log ("Prev Y: " + prev_y + "| New velocity after: " + new_velocity.y);
         controller.Move(new_velocity * Time.deltaTime);
     }

And this solved both of my problems, so it shall be marked as solved.

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

2 People are following this question.

avatar image avatar image

Related Questions

Do character controllers work with dynamic gravity? 1 Answer

Loss of gravity and interation with box collider 1 Answer

Problem using a position determined from onTrigggerEnter in Update 2 Answers

Gravity switch 1 Answer

how to apply orbit to the player 2 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