- Home /
Jump Input Sensitivity
(Sorry to the moderators, I deleted this post thinking I fixed it but I was mistaken)
Hello,
First i'd like to say pardon my amateurish coding, i'm still new to this. I'm trying to add sensitivity to my jump input for a 2D platformer so that the longer the jump button is held, the higher you jump.
I've been messing around with it all day, and have somewhat gotten results, however the jump is still a little unrefined and often times will not return the correct jump height (The jump button held down will sometimes result in a short jump and other times a long jump, for example). Based on my code, what might I need to change/add in order to allow a short jump for a simple press and a large jump for a constant press? Maybe there's a better method you would recommend?
My code is based on a rigidbody FPS script posted on the Unity wiki that I liked, modified for a 2D platformer. I've tried both Input.GetAxis("Jump") with low sensitivity as well as a custom float that adds while the jump button is held, capping out with a clamp, both with the same results of a random jump height.
Here's a code snippet of the better method, using the float that adds while the space bar is pressed. It changes the jump height, however it seems to be random rather than dependent on how long the jump button is held.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Rigidbody))]
public class JackMoves : MonoBehaviour
{
public bool letsJump = false;
public bool grounded = false;
public float jumpStrength = 0f;
public float jumpStrengthAdd = 0.2f;
public float jumpVariable;
public float jumpHeight = 5.5f;
public float gravity = 36.0f;
void Update()
{
// Jump
if (grounded == true && Input.GetKeyDown ("space"))
{
letsJump = true;
}
// Not jumping
if (grounded == false || Input.GetKeyUp ("space"))
{
letsJump = false;
}
// Gets sensitivity on the Space Bar press
if (Input.GetKey ("space"))
{
jumpStrength += jumpStrengthAdd;
jumpStrength = Mathf.Clamp(jumpStrength, 0, 2);
jumpVariable = Mathf.Sqrt(jumpStrength * jumpHeight * gravity);
}
// Resets the sensitivity
if (Input.GetKeyUp ("space"))
{
jumpStrength = 0f;
jumpVariable = 0f;
}
}
void FixedUpdate()
{
// Jump
if (letsJump == true)
{
rigidbody.velocity = new Vector3(0, jumpVariable, 0);
}
}
Thanks.
jumpStrength
will reach 2 VERY fast when it's incremented with 0.2 on every frame, try decreasing jumpStrengthAdd
.
Your answer
Follow this Question
Related Questions
Player object falling slowly when holding input 2 Answers
What's wrong with this script? 4 Answers
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
How do I make my character jump over a cube with rigid body moving towards it 1 Answer
ForceMode.Impulse Doesnt always fire. 2 Answers