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 alexkaskasoli · Apr 16, 2014 at 04:12 PM · charactercontrollerjumpsmoothsmoothly

Getting a smooth jump with the Character Controller

I'm embarrassed to post this here, as there are already several instances of this question on the boards but sadly no proper answers. This is the Update method of my PlayerControl script:

 void Update()
     {
         moveDirectionX = 0f;
         moveDirectionY = 0f;
 
         // Check Ground
         grounded = Physics.Raycast(transform.position, -transform.up, deltaGround, 1 << LayerMask.NameToLayer("Environment"));
 
         // Check Horizontal Movement
         float h = Input.GetAxis("Horizontal");
         moveDirectionX += h * moveForce;
 
         // Check Jump
         if (Input.GetKeyDown (KeyCode.Space) & grounded) {
             jumping = true;
             moveDirectionY += jumpForce;
         }
 
         // Add Gravity
         if (!grounded)
             moveDirectionY += gravityForce;
 
         // Move the controller
         moveDirection = new Vector3(moveDirectionX * Time.deltaTime, moveDirectionY * Time.deltaTime);
         cc.Move(moveDirection * Time.deltaTime);
     }

The part commented as // Check Jump shows how I'm currently handling the jump, basically by adding "JumpForce" to the vertical movement.

This is the method shown in quite a few posts on how to handle a jump with the Character Controller. But as far as I can tell, this is a horrible solution.

After reaching a certain height, the character falls pretty smoothly with the gravity. That is OK.

The problem is getting to that height. The character "teleports" (actually he's not teleporting he's just moving ridiculously fast) to the max height.

How can you get a smooth jump with a CharacterController?

I'm talking a jump that looks decent in like a Mario game.

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
0

Answer by invitadoxr · Jun 17, 2020 at 09:45 AM

moveDirection += jumpForce - gravity * Time.deltaTime. this help a litle bit. but maybe is not enought. i searching the same question.,this help a litle bit.

moveDirectionY += jumpForce - gravity * Time.deltaTime

But maybe is not enough. Iam in the search of the same solution.

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
avatar image
0

Answer by FeedMyKids1 · Jun 16, 2020 at 11:58 PM

Honestly, you'll want to just use a RigidBody because it'll take care of everything for you. And you can adjust the gravity in the ProjectSettings.Physics tab.

Here you create an object with a RigidBody attached and use RigidBody.AddForce method and provide a (direction * force) and a ForceMode (Impulse is best for jumping).

using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class TestCharContrl : MonoBehaviour { private Rigidbody rb = null;

 [SerializeField]
 private float horizSpeed = 5.0f;

 [SerializeField]
 private float jumpPower = 10.0f;

 private bool grounded = true;

 private void Start()
 {
     rb = GetComponent<Rigidbody>();
 }

 void Update()
 {
     //If on the ground and not moving up or down
     if (!grounded && rb.velocity.y == 0)
         grounded = true;

     if (grounded && Input.GetKeyDown(KeyCode.Space))
     {
         rb.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
     }

     float horizInput = Input.GetAxis("Horizontal");

     if (horizInput != 0)
         rb.AddForce(Vector3.right * horizInput * horizSpeed, ForceMode.Force);
 }

}

If your character is something like a cube and it's doing a bunch of flips/rolls, go to the RigidBody component and open the Constraints dropdown and freeze its Z rotation.

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
avatar image
0

Answer by FrameXdropProvrex · Jun 17, 2020 at 05:57 PM

I solved it by already calculating the gravityforce on the vertical speed while going up. I hope this helps:

     //Lets the player be able to jump when grounded.
     if (characterController.isGrounded)
     {
         verticalSpeed = -1;
         if (Jump.state)
         {
             verticalSpeed = JumpForce;
         }
     }

     //Calculating gravity so the player can fall off objects and returns to earth after a jump.
     verticalSpeed -= GravityForce * Time.deltaTime;
     movement.y = verticalSpeed;
     characterController.Move(movement * Time.deltaTime);
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

23 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

Related Questions

How to make smooth jump? 1 Answer

2D box collider (character floating) 1 Answer

Unity my player does not jump 1 Answer

(Solved) Character Floats After Hitting Head on Object 1 Answer

Jumping Sideways 1 Answer


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