- Home /
,How to disable autojumping when holding spacebar?
Hello Everyone,
I have some problems with my character controller. I want it to only jump once per spacebar press, such that it doesn't "bunnyhop" if you hold spacebar.
Can anyone take a look at this code and look for a solution? I can't come up with one. Also I'd greatly appreciate errors and things that could be done better being pointed out. Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement_Player : MonoBehaviour
{
public float speed = 10f; // walking speed
public float jumpSpeed = 21f; // initial jump speed, added to deltaY if jumped
public float gravity = -10f; // value to be subtracted from deltaY
public float accel_const = 0.5f; // constant at which gravity increases while falling
float deltaY = 0;
bool spaceconfirm = false;
private CharacterController _charCont;
// Use this for initialization
void Start()
{
_charCont = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
float deltaX = Input.GetAxis("Horizontal") * speed;
float deltaZ = Input.GetAxis("Vertical") * speed;
Vector3 movement = new Vector3(deltaX, 0, deltaZ);
if (Input.GetAxisRaw("Jump") == 0 && Grounded())
spaceconfirm = false;
DynGravity();
if (Input.GetAxisRaw("Jump") == 1 && Grounded())
spaceconfirm = true;
StartCoroutine(JumpEvent());
deltaY += gravity;
Vector3 jumping = new Vector3(deltaX, deltaY, deltaZ);
movement = Vector3.ClampMagnitude(movement, speed);
movement *= Time.deltaTime;
movement = transform.TransformDirection(movement);
_charCont.Move(movement);
jumping *= Time.deltaTime;
jumping = transform.TransformDirection(jumping);
_charCont.Move(jumping);
}
private IEnumerator JumpEvent()
{
do
{
if (spaceconfirm)
deltaY = jumpSpeed;
else
{
deltaY = 5f;
if (deltaY < 20f)
deltaY += 1.3f;
}
yield return null;
} while (!Grounded());
}
private IEnumerator FallEvent()
{
do
{
deltaY = 0;
yield return null;
} while (!Grounded());
}
bool Grounded() // returns true if on the ground
{
return Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), 1.2f);
}
void DynGravity() // function for dynamic gravity, its acceleration
{
if (Grounded())
gravity = -10f;
else
{
gravity -= accel_const;
if (gravity < -50)
gravity = -50;
}
}
}
,Hello everyone.
With this character controller the character keeps jumping when holding spacebar, and I want it to jump only if spacebar is pressed when on the ground, so that you can't "bunnyhop" by holding spacebar.
Can anyone take a look at this code and come up with a solution? Also if you find mistakes or something I could improve on, pointing it out would be really appreciated. Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement_Player : MonoBehaviour
{
public float speed = 10f; // walking speed
public float jumpSpeed = 21f; // initial jump speed, added to deltaY if jumped
public float gravity = -10f; // value to be subtracted from deltaY
public float accel_const = 0.5f; // constant at which gravity increases while falling
float deltaY = 0;
bool spaceconfirm = false;
private CharacterController _charCont;
// Use this for initialization
void Start()
{
_charCont = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
float deltaX = Input.GetAxis("Horizontal") * speed;
float deltaZ = Input.GetAxis("Vertical") * speed;
Vector3 movement = new Vector3(deltaX, 0, deltaZ);
if (Input.GetAxisRaw("Jump") == 0 && Grounded())
spaceconfirm = false;
DynGravity();
if (Input.GetAxisRaw("Jump") == 1 && Grounded())
spaceconfirm = true;
StartCoroutine(JumpEvent());
deltaY += gravity;
Vector3 jumping = new Vector3(deltaX, deltaY, deltaZ);
movement = Vector3.ClampMagnitude(movement, speed);
movement *= Time.deltaTime;
movement = transform.TransformDirection(movement);
_charCont.Move(movement);
jumping *= Time.deltaTime;
jumping = transform.TransformDirection(jumping);
_charCont.Move(jumping);
}
private IEnumerator JumpEvent()
{
do
{
if (spaceconfirm)
deltaY = jumpSpeed;
else
{
deltaY = 5f;
if (deltaY < 20f)
deltaY += 1.3f;
}
yield return null;
} while (!Grounded());
}
private IEnumerator FallEvent()
{
do
{
deltaY = 0;
yield return null;
} while (!Grounded());
}
bool Grounded() // returns true if on the ground
{
return Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), 1.2f);
}
void DynGravity() // function for dynamic gravity, its acceleration
{
if (Grounded())
gravity = -10f;
else
{
gravity -= accel_const;
if (gravity < -50)
gravity = -50;
}
}
}
Answer by Casiell · Aug 27, 2018 at 05:17 PM
You could use Input.GetButtonDown("Jump") instead of GetAxisRaw. It returns true only during the frame when user pressed the button and false on each subsequent frame
Answer by Lynn_245 · Apr 12, 2021 at 09:00 PM
using System. public class Movement_Player : MonoBehaviour. public float speed = 10f; // walking speed. public float jumpSpeed = 21f; // initial jump speed, added to deltaY if jumped. public float gravity = -10f; // value to be subtracted from deltaY. Nom Nom Dog Food Delivery
Answer by spiney199 · Apr 13, 2021 at 05:23 AM
While Input.GetButtonDown is one method as suggested before, this does cause issues when you want a button like input on something like a controller trigger.
A good solution I found on an older thread is to make an input manager like the one in this post. I modified it by making adding constructor that takes in a string (eg, the axes name) and instantiate it for each different input my game needs to take in. Thus you can use this to ensure the right behaviour for all your inputs, and also means your controls can be customised to just about anything.
Answer by NoobCarl · Apr 14, 2021 at 04:47 PM
You can use if(Input.GetKeyDown(KeyCode.Space) && Grounded())
instead of if (Input.GetAxisRaw("Jump") == 1 && Grounded())
Your answer
Follow this Question
Related Questions
jump with character controller best way? 1 Answer
Shift BHOP in Character Controller 0 Answers
Jumping not always work 2 Answers
Super Ghouls 'n Ghosts style jumps & double jumps? 1 Answer
Revise one conflicting code line? 1 Answer