Hi! So, I’m currently making my second game, a platformer, and I have a particle system set up called “PlayerJump” made so that when I double-jump the particle system instantiates. There is, however, one major bug. After the second jump, I can spam the jump key and it’ll instantiate more and more particle systems. This is annoying because it’s clearly a bug and I want to clean it up. Does anyone know how to fix it?
Here’s the code that instantiates the particle system:
if (isGrounded == false && Input.GetKeyDown(“up”))
{
anim.SetBool(“isJumping”, true);
Instantiate(PlayerJump, transform.position, Quaternion.identity);
}
Also here’s the whole script in case you need it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class P1Controller : MonoBehaviour
{
public float speed;
public float jumpForce;
private float moveInput;
private Rigidbody2D rb;
Animator anim;
private bool facingRight = true;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
private int extraJumps;
public int extraJumpsValue;
public GameObject PlayerJump;
// Start is called before the first frame update
void Start()
{
extraJumps = extraJumpsValue;
rb = GetComponent();
anim = GetComponent();
}
// Physics
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
moveInput = Input.GetAxisRaw(“Horizontal”);
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if(facingRight == false && moveInput > 0)
{
Flip();
}else if(facingRight == true && moveInput < 0)
{
Flip();
}
if(rb.position.y < -8f)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
// Main Controls
void Update()
{
anim.SetBool(“isJumping”, false);
if (isGrounded == true)
{
extraJumps = extraJumpsValue;
}
if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJumps–;
}else if(Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
if (moveInput != 0)
{
anim.SetBool(“isRunning”, true);
}
else
{
anim.SetBool(“isRunning”, false);
}
if (isGrounded == false && Input.GetKeyDown(“up”))
{
anim.SetBool(“isJumping”, true);
Instantiate(PlayerJump, transform.position, Quaternion.identity);
}
}
// Looking Left and Right
void Flip()
{
facingRight = !facingRight;
Vector2 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
This is what the bug looks like:
If anyone can help I will be so happy.
Thanks for your time.