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 Ramsden · Jan 27, 2017 at 06:29 PM · unity 5third person controller

Unity 5 - how to add a double jump to the default third person controller script ?

Hey, I've been trying to add a double jump to Unity's default third person controller script for a couple of days now with no success. I've tried to create a boolean to state that double jump is true after performing a normal jump and then false after the double jump but It ends up allowing me to jump and an infinite amount of times, I've also tried the counter method which ended with the same result. I'm going to assume I'm just missing something pretty obvious. but I was just wondering if anyone had a snippet that I could use if they have managed to achieve this I would be very grateful. Or even a bit of advice on something I may be missing. Thanks for your time.

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

3 Replies

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

Answer by Loui_Studios · Jan 28, 2017 at 02:59 PM

Ok. Here's a little walkthrough of how to add a simple double jump to the default third person character.

OK. So let's start disecting the third person character!

Open up ThirdPersonCharacter.cs and ThirdPersonUserControl.cs.

Firstly, you'll need to scan through the code and look for where the jump code starts, if you're lucky, there'll be a Jump() method, which makes things very easy. Otherwise, try hitting CTRL + F and searching for "grounded" or "ground". There should be a boolean that states whether the character is touching the ground or not. We can use this to narrow down where the jump code starts.

At line 46 I found the Move() method, which takes in a vector from the user and then moves them in that direction.

Inside the method is this code, which points to another method:

   if (m_IsGrounded)
       HandleGroundedMovement(crouch, jump);

And inside the HandleGroundedMovement() method, there's also this code:

 // check whether conditions are right to allow a jump:
 if (jump && !crouch && m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Grounded")) {
     // jump!
     m_Rigidbody.velocity = new Vector3(m_Rigidbody.velocity.x, m_JumpPower,  m_Rigidbody.velocity.z);
     m_IsGrounded = false;
     m_Animator.applyRootMotion = false;
     m_GroundCheckDistance = 0.1f;
 }

But finally, there's one more thing that should be modfied to achieve the double jump. In the ThirdPersonUserControl.cs script, you'll see that in a simple Update() loop there's an IF statement that detects jump input:

  private void Update() {
      if (!m_Jump)
          m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
  }

In a nutshell, this script detects user input and then feeds it into the ThirdPersonCharacter.cs script.

So at the top of this code, underneath m_Jump, add a public integer called "jumpState". This will count the amount of jumps we have done in midair. Since it's public, we can also see it in the inspector. Useful for debugging.

 public int jumpState;

What we will do is we will have the jumpState integer set to how many jumps you have done in midair. When jumpState goes over a limit, you can no longer jump in the air.

Ok so let's get this code to work. Modify the Update() loop to look like this:

 private void Update() {
     if (!m_Jump) {
         m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
         if (m_Jump)
             jumpState++;
     }
     if (m_Character.m_IsGrounded)
         jumpState = 0;
 }

Now open up the ThirdPersonCharacter.cs code and add this argument to the Move() method:

 int jumpState = 0

So now the Move() method should look like this:

 public void Move(Vector3 move, bool crouch, bool jump, int jumpState = 0) {

Then go back to the ThirdPersonUserContro.csl script, and in the FixedUpdate() loop, add this argument to the line that calls the move function. So now that line should look like this:

 m_Character.Move(m_Move, crouch, m_Jump, jumpState);

Finally, go back into ThirdPersonCharacter and make the m_IsGrounded variable public. Like this:

 public bool m_IsGrounded;

Now if you test the scene, you should notice that the value of jumpAmount sits at 0 when the character is touching the ground, and when you jump in mid air, that value increases.

Now to make the character jump in mid air!

So in the ThirdPersonCharacter.cs script, go down to the move function and add this line just before the if (m_IsGrounded) statement:

 if (jump == true && jumpState < 2) {
     HandleGroundedMovement(crouch, jump);
 }

Lastly, go down to the HandleGroundedMovement() method, and remove the animation check there, so now it will look like this:

 void HandleGroundedMovement(bool crouch, bool jump) {
     // check whether conditions are right to allow a jump:
     if (jump && !crouch) {
         // jump!
         m_Rigidbody.velocity = new Vector3(m_Rigidbody.velocity.x, m_JumpPower, m_Rigidbody.velocity.z);
         m_IsGrounded = false;
         m_Animator.applyRootMotion = false;
         m_GroundCheckDistance = 0.1f;
    }
 }

If done correctly, you will be able to jump a second time in mid air! To increase the number of jumps the character can do in mid air, just increase the number '2' here:

 if (jump == true && jumpState < 2) {
     HandleGroundedMovement(crouch, jump);
 }

If you want to, you can link this number to a variable and then have an upgrade system, or an on screen counter for how many jumps you have left.

Hope this helped. Let me know if the code works for you!

If not, just comment, and I'll help you sort it out!

Comment
Add comment · Show 18 · 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 Ramsden · Jan 28, 2017 at 08:12 PM 0
Share

Thank you so much, you guided me through it perfectly. I realised my mistake was that I wasn't putting it within the User Controls update thank you for all your help.

avatar image Loui_Studios Ramsden · Jan 28, 2017 at 08:13 PM 0
Share

You're welcome. I'm glad I helped you out! :)

avatar image Ramsden · Feb 01, 2017 at 04:52 PM 0
Share

I was just wondering, I understand if you don't want to help me any further, but I am attempting to change it so I can allow movement mid-air as I'm creating a 3D platformer. I handle all the combat in a separate script but currently, it makes the platfor$$anonymous$$g part a bit hard I've been reading through and adjusting the script for a couple of hours trying to get it to work. it's most likely me missing something obvious.

Thanks for helping me out again I've manage to make a lot of progress since inputting the double jump :)

avatar image srylain Ramsden · Feb 01, 2017 at 05:06 PM 1
Share

Someone can correct me if I'm wrong, which I probably am, but isn't the default Ethan animations root motion? That means the animations themselves control how he moves and because the falling animation doesn't move it's only controlled by physics at that point which is why he would continue moving but you can't change velocity/direction.

I'd think you should be able to disable the falling animation so he stays in the on-ground state (in the Animator window) and then you just need to be sure that you're still checking for player input and still moving him normally even though he's in the air. Although with doing that, it would look like he's just standing as he's falling through the air. Would take more code and Animator stuff to keep him in the falling animation if you're done moving.

Again, could totally be off the wrong track.

avatar image Loui_Studios srylain · Feb 01, 2017 at 05:28 PM 1
Share

@Ramsden Yes, cryneryan is indeed right.

The default third person character uses root motion to handle movement. Its handled from this method here:

 public void OnAnimator$$anonymous$$ove() {
             // we implement this function to override the default root motion.
             // this allows us to modify the positional speed before it's applied.
             if (m_IsGrounded && Time.deltaTime > 0) {
                 Vector3 v = (m_Animator.deltaPosition * m_$$anonymous$$oveSpeed$$anonymous$$ultiplier) / Time.deltaTime;
 
                 // we preserve the existing y part of the current velocity.
                 v.y = m_Rigidbody.velocity.y;
                 m_Rigidbody.velocity = v;
             }
         }

You'll have to remove this method and set m.Rigidbody.velocity yourself.

Show more comments
avatar image Loui_Studios Ramsden · Feb 02, 2017 at 02:43 PM 0
Share

@Ramsden No problem. Good luck!

avatar image Ramsden Loui_Studios · Feb 03, 2017 at 01:15 PM 0
Share

@Simply$$anonymous$$elee Hey, Sorry to bother you again but I gave your code a go and I couldn't really make it work, the character was floating above the map and I tried some code changes to the code, $$anonymous$$or changes in full honesty but I couldn't get it to work. As mentioned before I'm not the best of coders but I am slowly picking it up.

I was wondering if there was any chance of a hand with the mid-air movement as the default controller works great in every other aspect, and this would be the last change I would need to make for it.

Sorry to bother you once again :(

Show more comments
Show more comments
avatar image KaizerGoreng · Jul 14, 2017 at 02:55 AM 0
Share

@Simply$$anonymous$$elee Hi, sorry to bother you but I downloaded the test scene you (the one with the capsule + cowboy hat) and am wondering how can I modify it so that when I turn from side to side, the character rotates as well? Like how when we walk and turn we actually turn to face the we're walking.

You have been a great help though thank you so much!

avatar image
0

Answer by KaizerGoreng · Jul 14, 2017 at 02:55 AM

@SimplyMelee Hi, sorry to bother you but I downloaded the test scene you (the one with the capsule + cowboy hat) and am wondering how can I modify it so that when I turn from side to side, the character rotates as well? Like how when we walk and turn we actually turn to face the we're walking.

You have been a great help though thank you so much!

Comment
Add comment · Show 5 · 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 Loui_Studios · Jul 14, 2017 at 12:22 PM 0
Share

That's not too hard to do. There are a few ways you can do this.

Firstly, you'll need a motion vector. This motion vector is created at the bottom of the script for your convenience:

 Vector3 motion = new Vector3(horizontal * horizontalSpeed, jumpSpeed, forward * forwardSpeed); // Take our information and put it into a movement vector.

Afterwards, you'll simply want to adjust your character's y rotation using Quaternion.FromToRotation to calculate the difference between the current direction you're facing and the target direction.

A quick example:

 float angleY = Quaternion.FromToRotation(transform.forward, motion).y;

then simply add that rotation to your current y rotation and you should be good.

You may feel like reordering the code a bit so that all of the rotation code happens in one place, rather than in two different parts of the script, and it should work functionally the same. Infact, it's more optimised to batch transformations in that way.

avatar image KaizerGoreng Loui_Studios · Jul 17, 2017 at 04:26 AM 0
Share

Hey thanks for the fast reply!

I tried and did as you said, but I do not think it works how it should be. I replaced the "rot" Quaternion's y parameter from transform.rotation.y to angleY.

Am I doing it correctly?

 Vector3 motion = new Vector3(horizontal * horizontalSpeed, jumpSpeed, forward * forwardSpeed); // Take our information and put it into a movement vector.
 
         // If we move, set the rotation to the direction of the camera.
         if (horizontal != 0 || forward != 0)
         {
             // Set our rotation to the camera's
             transform.forward = cam.transform.forward;
 
             float angleY = Quaternion.FromToRotation(transform.forward, motion).y;
 
             //Correct the rotation so that the character is only rotated in the Y axis.
             Quaternion rot = new Quaternion(transform.rotation.x, angleY, transform.rotation.z, transform.rotation.w);
             rot.x = 0; rot.z = 0;
             transform.rotation = rot;
         }
avatar image Loui_Studios KaizerGoreng · Jul 18, 2017 at 01:28 PM 0
Share

Ah, my mistake. I'm not too well versed in quaternion math, and I'm still learning the functions as best I can.

I find it hard to explain what it is that I mean to say, so I'll show you the code that I'm using for one of my projects right now to get the AI enemies to rotate to face their target.

 Vector3 rot = transform.eulerAngles;
 rot.y += Quaternion.FromToRotation(transform.forward, aiBase.target.position - transform.position).eulerAngles.y;
 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(rot), Time.deltaTime * aiBase.reference.turnSpeed);

In your case, I think the problem is caused by the motion vector being relative to the player's local coordinates and not their world space coordinates, so the player could end up facing a point near to the center of the map depending on their speed.

For this we will want to use:

 transform.TransformDirection(motion);

to convert the motion vector into world coordinates for us to use.

Try that out first, and let me know how it works for you. Feel free to use the code example I gave you in your project, or as a reference to help with the scripting.

Also, check out the documentation for Quaternion.FromToRotation() and transform.TransformDirection().

https://docs.unity3d.com/ScriptReference/Quaternion.FromToRotation.html

https://docs.unity3d.com/ScriptReference/Transform.TransformDirection.html

Show more comments
avatar image
0

Answer by azgoodaz · Jun 02, 2019 at 07:40 AM

Since Unity deprecates code every update, this double jump code @SimplyMelee does not work anymore.

Is there a newer way how to make Ethan double jump in 2019?

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Asset Prefab Bug ThirdPersonController (left rotating bug / Snapping forward Always) 3 Answers

I need help with sliding in infinite Runner please someone help 1 Answer

Help! How do we enable or disable Collider with a specific action? 2 Answers

Third Person Controller Movement on Elevated Terrain 1 Answer

make Ethan (from unity standard assets) like temple run character 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