Search Unity

Can someone help me fix this particle bug?

Discussion in 'Scripting' started by Moonwatchereclipse, May 15, 2021.

  1. Moonwatchereclipse

    Moonwatchereclipse

    Joined:
    Jan 31, 2021
    Posts:
    3
    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<Rigidbody2D>();
    anim = GetComponent<Animator>();
    }

    // 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.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    First, use [ CODE ] tags.

    Second, move the particle instantiation and animation logic inside with the rest of your "check if they can actually jump" code that modifies the velocity. Having it by itself is the source of your bug, as you're not checking "can they actually jump" before you play the particles.
     
  3. Moonwatchereclipse

    Moonwatchereclipse

    Joined:
    Jan 31, 2021
    Posts:
    3
    ack, I already fixed the bug, but you are amazing for helping out :)
     
    GroZZleR likes this.