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 /
avatar image
0
Question by Caldeira · Dec 09, 2011 at 01:53 AM · cameraplayerjumpjumping

Having a problem with player jumping

I'm in the process of implementing a simple third person controller, but for some reason I'm having trouble getting the jumping to work smoothly.

Currently when the player jumps, instead of smoothly accelerating to a peak, then falling to the ground, they just appear at the peak of the jump, then slowly fall.

Here's the code.


    void Update () {
     
     cameraTransform = Camera.main.transform;
     
     Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
     forward.y = 0;
     forward = forward.normalized;
     
     Vector3 right = new Vector3(forward.z, 0, -forward.x);
     
     float v = Input.GetAxis("Vertical");
     float h = Input.GetAxis("Horizontal");
     
     targetDirection = h * right + v * forward;        
     
     
     if(targetDirection != Vector3.zero){
         moveDirection = targetDirection.normalized;
         
         lastFacingDirection = moveDirection;
         transform.forward = Vector3.Normalize(moveDirection);
     }
     else {
         moveDirection = Vector3.zero;
         transform.forward = Vector3.Normalize(lastFacingDirection);
     }
             
     if( grounded ) {            
         Jump();            
     }     
     
     ApplyGravity();    
     Move();            
     
 }



 private void Jump(){
     if(Input.GetButtonDown("Jump") ){
         moveDirection.y = jumpHeight;
     }
 }
 
 private void ApplyGravity(){
     moveDirection.y -= gravity * Time.deltaTime ;
 }
 
 private void Move(){
     
     moveDirection *= runSpeed;
             
     CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime);

     grounded = (flags & CollisionFlags.Below) != 0;        
 }



If I move the player using a separate vector which only changes when the jump button is pressed, I can get normal jumping to work, so the problem lies somewhere within the vector which changes based on the direction the camera is facing (vital to the project)

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 syclamoth · Dec 09, 2011 at 02:15 AM

The problem here is that you aren't modelling your character's movement in a complex enough way. Instead of using an instantaneous 'moveDirection' value, you should use something like 'velocity' which gets applied every frame. When the character is in the air, you should apply gravity in the same way, only using the velocity value instead of just the current movement. As it is, gravity is a constant velocity, not an acceleration (the way it is in reality). What you are doing at the moment, is instantly teleporting the player into the air when they press the jump button, but what you should be doing is treating your gravity like this-

 private void ApplyGravity(){
     if(!grounded)
     {
         velocity.y -= gravity * Time.deltaTime ;
     }
 }

And then when you jump, do this-

 private void Jump()
 {
     if(grounded)
     {
         velocity.y += jumpPower;
         grounded = false;
     }
 }

Of course, you won't be able to specify a jump height just with that- but look up gravitational acceleration equations on wikipedia for ways of getting around that (since you have no drag or friction in the air, it should be pretty straighforward.

Comment
Add comment · Show 3 · 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
avatar image syclamoth · Dec 09, 2011 at 02:16 AM 0
Share

If you want only gravity to be physics-affected, you can make the 'velocity' value one-dimensional, and then add it to the result of your character-controlling stuff before you actually apply the move!

avatar image Caldeira · Dec 09, 2011 at 08:09 PM 0
Share

Thanks very much, works a treat.

avatar image syclamoth · Dec 10, 2011 at 12:15 AM 0
Share

Can you mark the answer as accepted?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem between my player jump and player movement 1 Answer

Detecting Collision for Jumping 1 Answer

Player wont jump (visual script),2d player does not jump (visual script) 1 Answer

Why does my character never stop jumping? 0 Answers

How to prevent player from moving in air while jumping in place?? 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