Question by
InfernoGamingYT · Feb 01, 2021 at 03:28 AM ·
double jump2dplatformer
double jump
so, im fairly new to coding (i just started back up, so im pretty fuckin rusty), and i forget how to make my character double jump, this script has a wonky double jump, im looking for a clean and smooth double jump(not sure if i should share my whole code or just the jumping so ill do the entire code), this is for a 2d platformer game by the way(most of this code is pretty much stolen and edited, im bad i know lol), oh and the flip mechanic doesnt work idk why
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed;
public float jump;
float moveVelocity;
bool grounded = true;
void Update()
{
//Jumping
if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.W))
{
if (grounded)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jump);
}
}
moveVelocity = 0;
//Horizontal/Left and Right Movement
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
moveVelocity = -speed;
}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
moveVelocity = speed;
}
GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
grounded = true;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
walk animation to run animation please help 0 Answers
Double jump feels really off 1 Answer
[Solved] Zero out Velocity before double jumping? 0 Answers
double jump - max once 1 Answer