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 ogotera · Sep 12, 2017 at 06:25 AM · physicsgravityjump

My jump script doesn't work every time

Hey I am trying to write a jump script, the problem is that when I press the P key sometimes the character jumps and sometimes it doesn't. I would appreciate any help I can get.

Here is the code:

     public void Update()
 {
     

     if (Controller.isGrounded) {
         if (Input.GetKeyDown (KeyCode.P)) {
             vSpeed = JumpSpeed;

         }
     } 


 


     vSpeed -= Gravity * Time.deltaTime;
     MoveDirection.y = vSpeed;
 

 }
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 nt314p · Sep 13, 2017 at 11:44 PM 0
Share

$$anonymous$$aybe Controller.isGrounded is not always true.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by StevenUnu · Sep 12, 2017 at 06:54 AM

Try using a rigidbody and AddForce:

 public void Update() {
     if(Controller.grounded) {
          if(Input.GetKeyDown(KeyCode.P))
              rb.AddForce(Vector3.up * jumpSpeed * Time.deltaTime) ;
     }
 }



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 ogotera · Sep 12, 2017 at 08:13 PM 0
Share

Hi, thanks for your answer! I tried it but it doesn't do anything. Am I missing something?

avatar image Litleck ogotera · Sep 12, 2017 at 08:39 PM 0
Share

@ogotera did it give you an error if so please tell me.

avatar image
0

Answer by ogotera · Sep 12, 2017 at 09:07 PM

No error, just doesn't jump

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 Litleck · Sep 12, 2017 at 10:18 PM

Make sure you have a rigid body on your character and it has a mass of 1 then make a script and name it Controller with this script.

 using UnityEngine;
 
 public class Controller : MonoBehaviour
 {
     public float jumpPower = 5;
     private bool grounded;
 
     Rigidbody rbody;
     private void Start()
     {
         rbody = this.GetComponent<Rigidbody>();
     }
     void Update()
     {
         if (!grounded && rbody.velocity.y == 0)
         {
             grounded = true;
         }
         if (Input.GetKeyDown(KeyCode.P) && grounded == true)
         {
             rbody.AddForce(transform.up * (jumpPower*100));
             grounded = false;
         }
     }
 }

It isn't perfect because if spammed you can double jump but it works and hopefully it can help you and it'll be further improved.

Hope this helps!

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 ogotera · Sep 13, 2017 at 01:36 AM 0
Share

The script works but I would like to use it with a movement script using a Character Controller. If I use both then the jump script doesn't work anymore. Any ideas?

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

118 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

Related Questions

gravity doesnt seem to effect my first person controller....help! 1 Answer

The faster it goes, the longer it jumps 1 Answer

Edit this script so that on the second jump change the gravity to 5 2 Answers

The faster my character goes the longer he jumps. 1 Answer

How to make a object jump constantly at y and move to the next position to z (perfectly) 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