Jumping for longer 2d game
Hi There
I'm a complete beginner to unity and scripting, I'm trying to make a basic 2d running game, and would like the character to jump for longer while any key is held down. Getting help from tutorials and Unity Forum I've got a input script that acknowledges if the character is grounded, and an action button that is anyKeyDown, and have the below script to make the character jump, however at the moment the player jumps the same maximum height no matter how long the key is held for, any help would be very much appreciated
Thanks
using System.Collections; using UnityEngine;
public class Jump : MonoBehaviour {
public float jumpShortSpeed = 100f;
public float jumpSpeed = 300f;
public bool jump = false; public bool jumpCancel = false;
private Rigidbody2D body2d; private InputState inputState; void Awake() { body2d = GetComponent(); inputState = GetComponent(); }
void Update() { if (inputState.standing) { if (inputState.actionButton && inputState.standing)
jump = true; if (!inputState.actionButton && !inputState.standing)
jumpCancel = true; } } void FixedUpdate() {
if (jump)
{
body2d.velocity = new Vector2(body2d.velocity.x, jumpSpeed);
jump = false;
}
if (jumpCancel)
{
if (body2d.velocity.x > jumpShortSpeed)
body2d.velocity = new Vector2(body2d.velocity.x, jumpShortSpeed);
jumpCancel = false;
}
}
Your answer
Follow this Question
Related Questions
,Fps Controller jumps automaticalley 0 Answers
I am slowly falling down with the jump animation 4 Answers
While (starting jumping or in jump) holding W key makes game object jump high and fall slow. 1 Answer
Jump, if I touch button,Jump if I pres the touch button 0 Answers
How to double jump? i just can jump once when it touch the ground.. help me guys =D 1 Answer