- Home /
Disable jumping more than once
So today I started working on my little android game but I ran into a problem whit my moving script. Whenever I jump I will be able to jump more than once.I have attached the script to the player and all that to a button, TY! Code:
using UnityEngine; using System.Collections;
public class Move : MonoBehaviour {
// Use this for initialization
void Start () {
;
}
// Update is called once per frame
void Update () {
rigidbody2D.AddForce (Vector2.right * 10);
}
public void Groundand(Collider2D coll){
if (coll.gameObject.name == "ground") {
rigidbody2D.velocity = transform.up * 10;
}
}
}
Couldn't you just have an if statement, like this:
if(grounded==true){
jump();
}
else{
//do nothing
}
Grounded would be a variable that states if the player is grounded (obviously) and jump() would be a void to jump.
@Lucas$$anonymous$$ars but how would I check if they are coliding?
Attach colliders to the ground and the enemy. And paste the method OnCollisionEnter() to you script.
Or if you don't need a grounded player for your game but you know how long your jump animation takes when you jump (let's call that float JumpLength) set a
bool isJumping = true;
when you start jumping and set a
float JumpStartTime = Time.time;
The in Update()
Update()
{
if(isJumping)
{
if(Time.time > JumpStartTime + JumpLength)
isJumping = false;
}
Only allow jumping if isJumping = false
Really though you probably do want to know if the player is on the ground.
Of course $$anonymous$$arion/Sonic type games allow air jumping so it depends on how you want your game to function.
Answer by jpthek9 · Dec 26, 2014 at 10:08 PM
Plug this into your script and it should work. Basically, an integer counts how many jumps you can do and doesn't let you jump if you have none left. Every time you hit the ground, the integer resets and you get another set of jumps.
int JumpCount = 0;
public int MaxJumps = 1; //Maximum amount of jumps (i.e. 2 for double jumps)
void Start()
{
JumpCount = MaxJumps
}
void Update()
{
if (Input.GetButton("Jump"))
{
if (JumpCount > 0)
{
Jump();
}
}
}
public void Jump()
{
rigidbody2D.velocity = transform.up * 10;
JumpCount -= 1;
}
void OnCollisionEnter2D(Collision2D Col)
{
if (Collision.gameObject.tag == "ground")
{
JumpCount = MaxJumps;
}
}
Didn't work since the buttons im using are the UI buttons. So when I add everything and change the button name to Jump it still doesn't work.
So you created a button in the UI that, when clicked, should make it jump.
Follow @jpthek9 example but remove the lines in Update.
On you're UI (I'm assu$$anonymous$$g 4.6) select the jump button.
Click on AddComponent in the inspector and add an EventTrigger and click on the + just below it. Add OnPointerClick event then drag whatever GameObject the script is attached to into the empty slot that appears.
From the dropdown box to the right select $$anonymous$$ove -> Jump
That's assu$$anonymous$$g your script is still called $$anonymous$$ove.
When you click it should trigger the Jump function.
Did I really deserve a down vote? Obviously you just change the method of input. Paste this in place of the update.
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 50, 50), "Jump")) //$$anonymous$$ake your button here
{
if (JumpCount > 0)
{
Jump();
}
}
}
And your script doesn't work because OnCollisionEnter2D is what gets called when you have 2d colliders colliding, not GroundCheck.
@jpthek9 Stupid question but why would I create a button whit the old GUI since I already made a button whit the new UI?
You don't but also @jpthek9 didn't deserve a down vote.
You didn't mention that you were using UI buttons in the original post so that solution would be valid for the original answer given all that was known at the time.
Answer by kawales · Dec 27, 2014 at 10:16 AM
Yay I found the answer! For all the people that are curious heres the code I made a new script just fro jumping even tho I didn't have to do that :P
Code:
using UnityEngine; using System.Collections;
public class JumpScript : MonoBehaviour { public int JumpCount = 1; // Use this for initialization void Start () {
} // Update is called once per frame void Update () { } public void GroundCheck(Collider2D coll){ if (coll.gameObject.name == "ground" && JumpCount == 1) { rigidbody2D.velocity = transform.up * 10; JumpCount = JumpCount - 1; } else if (coll.gameObject.name == "ground" && JumpCount == 0) { JumpCount = JumpCount + 1; } }
Nvm I now have even more problem cuz when I click jump it jumps normally then when i click the next time he doesn't jump but if i spam it will jump every second time...
Answer by the1337Wolf · Jan 04, 2015 at 06:52 AM
conversely, you could also have a variable public bool isGrounded = true; set this to false in your jump function. Then create a function onCollisionEnter2D and tag the ground sprite with "Ground". In this function, check if the collision is with "Ground", If it is then set isGrounded to true. Only allow the player to jump if isGrounded is True.
Answer by mreina · Jun 06, 2020 at 02:40 PM
Hello,
Another possibility is the following:
Create a public method in your character controller script, which will disable the jumping
// This method is called from an event in the jump animation, when the animation is finished
public void Land()
{
_isJumping = false;
}
Go to the Animator in Unity, double click on your jump animation.
Go the Event section
Add an event at the end of the animation timeline:
In the function field enter the nameof the your public function"Land"
In the Object field select your character controller script
I think that this should work even if you land on something that is not the ground.
Regards
Your answer
![](https://koobas.hobune.stream/wayback/20220613174930im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make it to where pressing a button on screen doesn't shoot a projectile? 1 Answer
Android 2D Character Controller 2 Answers
2D multi touch 1 Answer
After exporting my game stopped working 0 Answers
Color area below line renderer? 0 Answers