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 /
  • Help Room /
avatar image
0
Question by vaniokrika · Oct 13, 2017 at 07:30 PM · jumping

Spam jumping problem!

So my jump script is working but if I spam the jump button the player will keep going up. Here is my jumping script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerJump : MonoBehaviour
 {
     void Update()
     {
         if (Input.GetKey(KeyCode.Space))
         {
             GetComponent<Rigidbody>().velocity = Vector3.up * JumpForce;
         }        
     }
 }
 

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Nivey · Oct 14, 2017 at 08:47 AM

Well this is because your are just executing jump whenever key is pressed. you can make another if statement checking if the player is on the ground at that time when pressed and only if this is true 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 vaniokrika · Oct 14, 2017 at 12:38 PM

ok but how

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 Pepetroll · Oct 16, 2017 at 06:40 PM

u need to declare you player is grounded

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 SideID · May 01, 2018 at 07:16 AM 0
Share

i did this and now it doesnt jump at all CODE -----------

using UnityEngine;

public class movement : $$anonymous$$onoBehaviour {

  private Rigidbody rb;
  public float jumpvelocity;
  float jumptime = 0f;
   
  public $$anonymous$$eyCode up; // The button is the letter q
  float adjsVelsocityY;
  public void Start()
  {
      rb = GetComponent<Rigidbody>();
  }
  public void FixedUpdate()
  {
      adjsVelsocityY = 0f;
    
      if (Input.Get$$anonymous$$eyDown(up))
      {
          if (jumptime >= 1) //the player can jump 1 time in the air
          {
              adjsVelsocityY = jumpvelocity;
              jumptime = jumptime + 1;
          }
          
      }
      
      rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y + adjsVelsocityY, rb.velocity.z);
      adjsVelsocityY = 0f;
  }
  public void OnTriggerEnter(Collider other)
  {
      if (other.CompareTag("ground")) //i did set the tag
      {
          jumptime = 0f;
      }
  }
avatar image Nivey · May 02, 2018 at 10:27 AM 0
Share

if (jumptime >= 1) //the player can jump 1 time in the air. i think this should be. if (jumptime == 0) //player is on the ground and can jump 1 time.

also i would not use the collider here to trigger a code. this would trigger it constantly when walking. i would make a boolean function i can ask to check before jumping like so (not at home so could not test it).

 private bool checkplayergrounded()
 {
     if (see i player is colliding with ground))
         {
              return true;
         }
     else
         {
             return false
         }
 }

now you can check this:

  if (Input.Get$$anonymous$$eyDown(up))
       {
           if (checkplayergrounded() == true) //the player can jump 1 time in the air
           {
               adjsVelsocityY = jumpvelocity;
           }
           
       }

now multi jump is not possible becouze the second press will return checkplayergrounded as false;

i hope this helps.

avatar image
0

Answer by SideID · May 02, 2018 at 07:16 PM

Not at my home, but thanks in advance. (im a beginner coder, if you help me to make the if statement, i would be really greatful )

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 Nivey · May 02, 2018 at 08:19 PM 0
Share

multiple ways depending on your level design. is it a flat surface? if so you can check player y to see if it is on the ground. if not like most games :P u can make a raycast at the start of your game to see the players distance to the ground. then when jump is pressed do the same raycast in a bool function if its greater return false else return true. would look something like this (got this from another thread).

 private float distancetoground;
 
 void start()
 {
     distancetoground= playercolliderhere.bounds.extents.y;
 }
 
 private bool IsGrounded()
 {
     return Physics.Raycast(playertransformhere.position, - Vector3.up, distToGround + 0.1f);
     //this should return a hit or miss so true or false. replace playercolliderhere and playertransformhere with your collider and transform name.
  }
 
 if (Input.Get$$anonymous$$eyDown(up) && IsGrounded())
 {
     adjsVelsocityY = jumpvelocity;
 }

again not tested and translated it from java

avatar image SideID Nivey · May 03, 2018 at 06:50 PM 0
Share

did this

public Collider Collider;

 public bool IsGrounded()
 {
     if (Collider.CompareTag("ground"))
     {
         return true;
     }else
     {
         return false;
     }
 }


     if (Input.Get$$anonymous$$eyDown(up))
     {

         if (IsGrounded = true)
         {

             adjsVelsocityY = jumpvelocity;

         }

Throws an error in the if statement "Can not assign to 'IsGrounded' because it is a 'method group' " hielp

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

120 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

Related Questions

vibrating jumps and collisions 0 Answers

Touch control swipe up to jump 1 Answer

My game seems to be running too fast for certain tasks 0 Answers

problem with jump script 2 Answers

Help please! Why can't I reduce the speed before jumping? 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