- Home /
Two-mode jumping
Working on a 2D platformer, and you know how in some games, the jump depends on how long the button is held? Well, I'd like to do that, with two different heights for jumping, a normal jump when the button is tapped versus a high jump when it's held for a certain amount of time. I'd also like it to begin the jump as soon as the button is pressed, and figure out mid-air whether it's a high-jump or not.
Unfortunately, I haven't had any luck even correlating what I'm doing with what's happening on the screen. Hoping someone can help.
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour
{
[HideInInspector]
public bool facingRight = true; // For determining which way the player is currently facing.
[HideInInspector]
public bool jump = false; // Condition for whether the player should jump.
public bool highJump=false;
public float jumpTimeThreshold = .5f;
public float jumpBeginTime = 0f;
public float jumpEndTime = 0f;
public float moveForce = 365f; // Amount of force added to move the player left and right.
public float maxSpeed = 5f; // The fastest the player can travel in the x axis.
public AudioClip[] jumpClips; // Array of clips for when the player jumps.
public float jumpForce = 200f; // Amount of force added when the player jumps.
private Transform groundCheck; // A position marking where to check if the player is grounded.
private bool grounded = false; // Whether or not the player is grounded.
private Animator anim; // Reference to the player's animator component.
void Awake()
{
// Setting up references.
groundCheck = transform.Find("groundCheck");
anim = GetComponent<Animator>();
}
void Update()
{
float v = Input.GetAxis("Vertical");
// The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
// If the jump button is pressed and the player is grounded then the player should jump.
if(Input.GetButtonDown("Jump") )
{
if(grounded){
if(v < 0)
{
transform.position += new Vector3(0, -0.8f, 0);
//Debug.Log(transform.position);
}
else{
jump = true;
//counter++;
jumpBeginTime=Time.fixedTime;
}
}
}
}
void FixedUpdate ()
{
// Cache the horizontal input.
float h = Input.GetAxis("Horizontal");
//float v = Input.GetAxis("Vertical");
if (Input.GetButton("Hold"))
{
// Stop the guy only if he's on ground
if(grounded)
{
rigidbody2D.velocity = new Vector2 (.5f, .5f);
}
}
else {
// The Speed animator parameter is set to the absolute value of the horizontal input.
anim.SetFloat("Speed", Mathf.Abs(h));
// If the player is changing direction (h has a different sign to velocity.x) or hasn't reached maxSpeed yet...
if (h * rigidbody2D.velocity.x < maxSpeed)
// ... add a force to the player.
rigidbody2D.AddForce (Vector2.right * h * moveForce);
// If the player's horizontal velocity is greater than the maxSpeed...
if (Mathf.Abs (rigidbody2D.velocity.x) > maxSpeed)
// ... set the player's velocity to the maxSpeed in the x axis.
rigidbody2D.velocity = new Vector2 (Mathf.Sign (rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
}
// If the input is moving the player right and the player is facing left...
if(h > 0 && !facingRight)
// ... flip the player.
Flip();
// Otherwise if the input is moving the player left and the player is facing right...
else if(h < 0 && facingRight)
// ... flip the player.
Flip();
// If the player should jump...
if(jump)
{
jumpTimeThreshold=.5f;
jumpEndTime=Time.fixedTime;
float jumpingTime=(jumpEndTime-jumpBeginTime);
Debug.Log("Amount of Time Pressed: B:"+jumpBeginTime+" E:"+jumpEndTime+" Diff:"+jumpingTime+" Threshold:"+jumpTimeThreshold);
if(Input.GetButtonUp("Jump") || jumpingTime>=jumpTimeThreshold){
// Set the Jump animator trigger parameter.
anim.SetTrigger("Jump");
// Play a random jump audio clip.
int i = Random.Range(0, jumpClips.Length);
AudioSource.PlayClipAtPoint(jumpClips[i], transform.position);
// Add a vertical force to the player.
if( jumpingTime>=jumpTimeThreshold){
rigidbody2D.AddForce(new Vector2(0f,jumpForce+500f));
}else{
rigidbody2D.AddForce(new Vector2(0f,jumpForce));
}
// Make sure the player can't jump again until the jump conditions from Update are satisfied.
jump = false;
highJump=false;
jumpBeginTime=0f;
jumpEndTime=0f;
}
}
}
void Flip ()
{
// Switch the way the player is labelled as facing.
facingRight = !facingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
A quick glance at this code seems to have what you want, a double jump timer started while in air, so..
What's not working/causing an error/etc? Are you getting the stuff you expected in Debug.Log?
The way it is now, he just sits on the ground and waits for the threshold or the button to be released. I actually understand why it's doing that, too. However, it's also randomly just waiting for the threshold to be crossed and then doing the super jump, even when I tap. If I change the threshold to 2 seconds and tap, it'll randomly wait 2 seconds and high jump. When that happens, I see in the log where the button pressed time is climbing.
Answer by JoshRose · Jan 25, 2014 at 04:23 PM
If I keep tapping jump to do regular jumps, sporadically it seems like he'll just wait on the ground for jumpTimeThreshold seconds and then do a high jump. I'll see the amount of time pressed log throw a bunch of rows with the threshold climbing upwards rather than the single row when it's acting like its supposed to.
When I legitimately hold the button, he waits on the ground until I release or the threshold is met. I actually think I understand why this is happening, but the sporadic thing is killing me.