• 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
1
Question by Logicalness · Jan 24, 2013 at 02:47 AM · c#velocityjumptrajectory

Move while jumping. Fun vs Realism or is there a "best of both worlds" solution?

Hello!

I understand the question I've posted is probably pretty confusing so I'll try to clear it up.

I have two functions, one of them (MovePlayerNoAirMove()) allows the player to jump, let go of all controls and maintain their trajectory until they land. The draw back is that they can not change direction while in the air. This is very realistic, but not too much fun. It feels like I'm punishing the player for jumping.

The other function (MovePlayerWithAirMove()) allows a player to change direction while in the air, however if you let go of all controls while in the air, your velocity immediately drops to zero and you fall to the ground. It looks like the character is hitting an invisible wall.

The only thing wrong with the second function (MovePlayerWithAirMove()) is the strange screeching halt in mid air effect. So when you're running straight, hit jump then let go of the analog stick, you would expect to stay on the same trajectory. Instead, the player just stops and falls. It's really odd looking.

I'm hoping for a "best of both worlds" solution. I'm thinking that I need to use different methods of applying gravity and moving the controller based on whether or not isGrounded is true. Right now I have two states, either you're on the ground or you're in the air. Do I need four states? Grounded moving/standing, Air moving/maintaining trajectory.

Any ideas?

Thanks!

(Here's the code for both functions.)

     // -- Directional movement (Can not move while in the air -- realistic) ---------------
     private void MovePlayerNoAirMove() {
         // Cache a Character Controller reference only if one does not exist
         if (!controller)
             controller = GetComponent<CharacterController>();
 
         // Movement when character is on the ground
         if (controller.isGrounded) {
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
             
             // Jump!
             if (Input.GetKey(KeyCode.Space) || Input.GetButton("Jump"))
                 moveDirection.y = jumpSpeed;
         }
         
         // Apply gravity and keep moving the character in the direction he was going
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
 
     }
 
     
     // -- Directional movement (Player can move while in the air after jumping -- fun) ------
     private void MovePlayerWithAirMove() {
         // Cache a Character Controller reference only if one does not exist
         if (!controller)
             controller = GetComponent<CharacterController>();
         
         // Setup move directions
         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;
     
         // Jump!
         if (controller.isGrounded) {
             jumpSpeedAirMove = 0;                 // a grounded character has zero vertical speed unless...
             if (Input.GetButton ("Jump")) {     // ...Jump is pressed!
                 jumpSpeedAirMove = jumpSpeed; 
             }
         }
 
         // Apply gravity and move the player controller
         jumpSpeedAirMove -= gravity * Time.deltaTime;
        moveDirection.y = jumpSpeedAirMove;
         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
1

Answer by Nanorock · Jan 24, 2013 at 12:20 PM

In your second function, you should get the speed decay, and not being set as bound only to input. I would add a var like previousSpeed.

something like (untested)

 newSpeed = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
 newSpeed = transform.TransformDirection(newSpeed);
 newSpeed *= playerSpeedFactor;

 if (!controller.isGrounded) {
     previousSpeed.X *= decaySpeed;
     previousSpeed.Z *= decaySpeed;
     previousSpeed.Y -= gravity;
 }
 else{// Jump!
     // a grounded character has zero vertical speed unless...
     if (Input.GetButton ("Jump")) {    // ...Jump is pressed!
         newSpeed.Y = jumpSpeed;
     }
 }

 moveDirection = newSpeed+previousSpeed; //Do some magic mixing between both speed
 moveDirection = Clamp(moveDirection) //This function doesn't exist, up to you to avoid the movement to get crazy
 previousSpeed = moveDirection;
 controller.Move(moveDirection * Time.deltaTime);

It's far far away from being good. Just merely pointing the fact that you need to maintain the player velocity variable.

Comment
Add comment · Show 1 · 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 danman3210 · Jan 02, 2016 at 02:06 AM 0
Share

it says unknown identifier: playerSpeedFactor

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Jump not working with Time.deltaTime 2 Answers

jump with character controller best way? 1 Answer

Jittery jumping on cellphone but not on pc. 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