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 HappyPants84 · Jul 08, 2018 at 09:30 PM · movementcharactercontrollermovement scriptjumping

How to make your character jump?

I watched a video that taught me how to make a character move around but they didn't say how to make the character jump and after get some weird results while testing to add a rigidbody I have given up on trying to figure it out myself and the internet hasn't been very helpful.

Here's the script btw:

 public float sensitivity = 5f;
 public float speedFaster = 10f;
 public float normalSpeed = 5f;

 float speed;

 CharacterController player;

 public GameObject eyes;

 float moveLR;
 float moveFB;

 float rotX;
 float rotY;

 void Start () {
     float speed = normalSpeed;
     player = GetComponent<CharacterController>();
     
 }
 
 void Update ()
 {

     if (Input.GetKey(KeyCode.LeftShift))
     {
         speed = speedFaster;
     }
     else
     {
         speed = normalSpeed;
     }
     

     moveFB = Input.GetAxis("Vertical") * speed;
     moveLR = Input.GetAxis("Horizontal") * speed;

     rotX = Input.GetAxis("Mouse X") * sensitivity;
     rotY = Input.GetAxis("Mouse Y") * sensitivity;

     Vector3 Movement = new Vector3(moveLR, 0, moveFB);
     Movement = transform.rotation * Movement;

     transform.Rotate(0, rotX, 0);
     eyes.transform.Rotate(-rotY, 0, 0);

     player.Move(Movement * 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
0

Answer by YBtheS · Jul 09, 2018 at 12:21 AM

The Unity Scripting API has a perfect example: CharacterController.Move. This only has an example in JavaScript so make sure that you set the language appropriately. You should be able to get the general idea from it.

You need to have a jump force variable (like public float jumpForce = anyNumber). After that, you need to check if you are on the ground (unless you want to be able to jump midair), then check if the space bar (or whatever your jump key is) has been pressed, then account for gravity, and finally send that to the player.Move() method. The new script should look something like this:

      public float sensitivity = 5f;
      public float speedFaster = 10f;
      public float normalSpeed = 5f;
      public float jumpForce = 10f; //You can change this number to whatever works right for your game.
      public float gravity = 1f;    //Same with this variable. I just choose random numbers. They most
                                    //most likely won't look right
      float speed;
      CharacterController player;
      public GameObject eyes;
      float moveUD
      float moveLR;
      float moveFB;
      float rotX;
      float rotY;
      void Start () {
          float speed = normalSpeed;
          player = GetComponent<CharacterController>();
          
      }
      
      void Update ()
      {
          if (Input.GetKey(KeyCode.LeftShift))
          {
              speed = speedFaster;
          }
          else
          {
              speed = normalSpeed;
          }
 
          if (Input.GetKeyDown(KeyCode.Space))
          {
              moveUD += jumpForce;
          }
          else
          {
              moveUD -= gravity;
          }
 
          moveFB = Input.GetAxis("Vertical") * speed;
          moveLR = Input.GetAxis("Horizontal") * speed;
          rotX = Input.GetAxis("Mouse X") * sensitivity;
          rotY = Input.GetAxis("Mouse Y") * sensitivity;
          Vector3 Movement = new Vector3(moveLR, moveUD, moveFB);
          Movement = transform.rotation * Movement;
          transform.Rotate(0, rotX, 0);
          eyes.transform.Rotate(-rotY, 0, 0);
          player.Move(Movement * Time.deltaTime);
      }

I hope this helps! :)

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

148 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

Related Questions

Make the Character Controller jump to the side 1 Answer

Strange Character Controller Behavior Caused by Simulated Gravity and Ground Check 0 Answers

My player keeps launching into the sky 1 Answer

Dampening Movement while in the Air 0 Answers

Moving AI away or towards player not working correctly. 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