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 ugotstoopt · Dec 08, 2020 at 06:47 AM · charactercontrollerjumpingmomentum

Maintaining forward momentum and control while jumping with character controller

Hello,

I am creating a 3D platformer and have created a character controller that allows the player to run and jump. The problem is when the player jumps all forward momentum is lost, the character just jumps straight vertically. I'm trying to have the player continue his/her forward momentum when jumping, and also allow them to control the character while in the air as that's a common/expected mechanic.

I've read several posts on Unity and Stack Overflow and can't find any similar questions/solutions. Can anyone help point out how I can accomplish this?

My code is almost all from Brackey's 3D movement tutorial with a few small customizations and animation code: (Link to his tutorial: https://www.youtube.com/watch?v=4HpC--2iowE).

While I understand how the existing code works (after many hours reading about Eulers and Quaternions), unfortunately I can't figure out how to both keep forward momentum while jumping and allow the player to control their position while in the air. Here is the code:

 public class MovePlayer : MonoBehaviour
 {
 
     CharacterController controller;
     Animator anim;  
 
     public Transform cam;
 
     public float moveSpeed = 5.0f;
     public float jumpHeight = 2f;
     public float gravity = 18.0f;
 
     public bool groundedPlayer;
 
     public Vector3 jumpVector = Vector3.zero;
 
     public float turnSmoothTime = 0.1f;
     float turnSmoothVelocity;
 
 
     void Awake()
     {
         controller = GetComponent<CharacterController>();
         anim = GetComponent<Animator>();
     }
 
     // Update is called once per frame
     void Update()
     {
         groundedPlayer = controller.isGrounded;
 
         float x = Input.GetAxisRaw("Horizontal");
         float z = Input.GetAxisRaw("Vertical");
         
         Vector3 moveDirection = new Vector3(x, 0f, z).normalized;
 
         if (moveDirection.magnitude >= 0.1f && groundedPlayer)
             {
 
             anim.SetBool("Moving", true);
 
             float targetAngle = Mathf.Atan2(moveDirection.x, moveDirection.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
 
             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
 
             transform.rotation = Quaternion.Euler(0f, angle, 0f);
 
             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;        
 
             controller.Move(moveDir.normalized * moveSpeed * Time.deltaTime);
             
             if (Input.GetButtonDown("Jump"))
             {
                 jumpVector.y = Mathf.Sqrt(jumpHeight * 2f * gravity);               
             }
 
         }
         else
             {
                 anim.SetBool("Moving", false);
             }
 
         jumpVector.y -= gravity * Time.deltaTime;
 
         controller.Move(jumpVector * Time.deltaTime);
 
     }
 
 }

Thank you in advance for any help you can offer.

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 ugotstoopt · Dec 08, 2020 at 04:22 PM 0
Share

Solution: I had to move the groundedPlayer check from the "if (moveDirection.magnitude >= 0.1f && groundedPlayer)" line and instead to the "if (Input.GetButtonDown("Jump"))" line.

New code below.

 public class $$anonymous$$ovePlayer : $$anonymous$$onoBehaviour
 {
 
     CharacterController controller;
     Animator anim;  
 
     public Transform cam;
 
     public float moveSpeed = 5.0f;
     public float jumpHeight = 2f;
     public float gravity = 18.0f;
 
     public bool groundedPlayer;
 
     public Vector3 jumpVector;
 
     public float turnSmoothTime = 0.1f;
     float turnSmoothVelocity;
 
 
     void Awake()
     {
         controller = GetComponent<CharacterController>();
         anim = GetComponent<Animator>();
     }
 
     // Update is called once per frame
     void Update()
     {
         groundedPlayer = controller.isGrounded;
 
         float x = Input.GetAxisRaw("Horizontal");
         float z = Input.GetAxisRaw("Vertical");
         
         Vector3 moveDirection = new Vector3(x, 0f, z).normalized;
 
         if (moveDirection.magnitude >= 0.1f )
             {
 
             anim.SetBool("$$anonymous$$oving", true);
 
             float targetAngle = $$anonymous$$athf.Atan2(moveDirection.x, moveDirection.z) * $$anonymous$$athf.Rad2Deg + cam.eulerAngles.y;
 
             float angle = $$anonymous$$athf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
 
             transform.rotation = Quaternion.Euler(0f, angle, 0f);
 
             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;        
 
             controller.$$anonymous$$ove(moveDir.normalized * moveSpeed * Time.deltaTime);
 
             if (Input.GetButtonDown("Jump") && groundedPlayer)
             {
                 jumpVector.y = $$anonymous$$athf.Sqrt(jumpHeight * 2f * gravity);
             }
 
         }
         else
             {
                 anim.SetBool("$$anonymous$$oving", false);
             }
 
         jumpVector.y -= gravity * Time.deltaTime;
 
         controller.$$anonymous$$ove(jumpVector * Time.deltaTime);
 
     }
 
 }

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ugotstoopt · Dec 08, 2020 at 06:49 AM

Also not sure why my code all shows up on a single line...

Comment
Add comment · Show 2 · 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 Llama_w_2Ls · Dec 08, 2020 at 07:41 AM 0
Share

You should leave a space between text and code block for it to be placed in a code block e.g. without space between text and code = void Start() {}

With space between text =

 void Start()
 {
 
 }

avatar image ugotstoopt Llama_w_2Ls · Dec 08, 2020 at 02:56 PM 0
Share

Thanks, got it

avatar image
0

Answer by Llama_w_2Ls · Dec 08, 2020 at 07:45 AM

This line public Vector3 jumpVector = Vector3.zero; means that jumpVector.x = 0 and jumpVector.z = 0;

This means that when you set the controller to move using the jumpVector on jump, it has no x or z magnitude. You need to modify the current move direction's y position, and not really setting the controller's move direction to another variable. @ugotstoopt

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 ugotstoopt · Dec 08, 2020 at 04:23 PM 0
Share

Thanks for the reply on this. I solved it by moving the location of the "groundedPlayer" check to the section looking for the 'jump' input.

avatar image
0

Answer by Virtoze · Dec 19, 2021 at 09:17 AM

Hi I know this is a bit late lol. But I found the reason behind it. I was getting the same problem. The problem is the playermovement with speed, is under the statement "is grounded" which means when you jump you are not grounded, and therefore the playermovement with the speed, is not valid. I moved the "move = speed" line further down out of the "if grounded", and let it run in the void update as a constant, and it works flawlessly.

Hope this helps you and/or others!

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

162 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

Related Questions

How to move while jumping? 1 Answer

Stop player momentum carrying over when using a 'portal'? 1 Answer

I can't do that my character jumps while running 0 Answers

Character Controller Jitters When Jumping Next To Objects 4 Answers

Character Controller jump doesn't work for some reason? 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