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;
}
}
}
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.
Answer by Pepetroll · Oct 16, 2017 at 06:40 PM
u need to declare you player is grounded
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;
}
}
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.
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 )
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
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
Follow this Question
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