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 Swagology · Apr 02, 2017 at 10:09 AM · physicsjumpheight

How do I make the player jump at a fixed height?

I've been tinkering with the code, but I can't find anything that works. I'm making a 3D game, and writing in C#. Here's the code that I have now.

 public class PlayerController : MonoBehaviour {
 
     public float speed;
 
     public float jumpHeight;
 
     private Rigidbody rb;
 
     void Start ()
     {
         //Got too lazy, so added this in.
         rb = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate ()
     {
         //Movement on the X and Z axes
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
 
         rb.AddForce (movement * speed);
 
         // The Jump Code
         if (Input.GetKeyDown (KeyCode.Space))
         {
             Vector3 jump =new Vector3 (moveHorizontal, jumpHeight, moveVertical);
 
             rb.AddForce (jump);
         }
     }
 }
 

Now what? I want to do this in the shortest and the most efficient way possible, without using too many variables.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by RobAnthem · Apr 02, 2017 at 11:02 AM

First suggestion, don't use AddForce for movement, it's basically the worst thing you can do to move a player. Use Velocity. After that, you can literally just AddForce to jump like this.

 rb.AddForce(transform.up * jumpHeight);

Or you can incorporate it into your velocity mechanism and scale or lerp the height over time to smooth it out. Also you need to smooth your movement out a bit. I wrote a tutorial on this, you can find it here. Proper Velocity-Based Movement 101

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 toddisarockstar · Apr 02, 2017 at 06:15 PM

if your game is 3d and Y is up, this should get you moving:

      public class PlayerController : MonoBehaviour {
      
          public float speed;
      
          public float jumpHeight;
      
          private Rigidbody rb;
      
          void Start ()
          {
              //Got too lazy, so added this in.
              rb = GetComponent<Rigidbody>();
          }
      
          void FixedUpdate ()
          {
              //Movement on the X and Z axes
              float moveHorizontal = Input.GetAxis ("Horizontal");
              float moveVertical = Input.GetAxis ("Vertical");
      
              Vector3 movement = new Vector3 (moveHorizontal, rb.velocity.y, moveVertical);
      
              rb.velocity=movement*speed;
      
              // The Jump Code
              if (Input.GetKeyDown (KeyCode.Space))
              {
 
      
                  rb.AddForce (Vector3.up*jumpHeight);
              }
          }
      }
      
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 Swagology · Apr 17, 2017 at 10:17 PM 0
Share

The movement is good, but when I jump, it seems to teleport to a higher location and descend slowly, as if there was no gravity at all.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

First person skydiver help 1 Answer

How to jump always with the same height 1 Answer

How to make a physics based jump with button hold effect 0 Answers

Jump with physics 1 Answer

why does my train jump off the tracks? 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