Why is my firepoint sometimes spawning 2 projectiles?

,

you coded it incorrectly

1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private Rigidbody2D rb;
    private Animator animator;
    private Weapon weapon; // Referenz auf das Weapon-Skript

    [SerializeField] private float moveSpeed = 5f;
    [SerializeField] private float jumpForce = 10f;
    [SerializeField] private Transform groundCheck;
    [SerializeField] private LayerMask groundLayer;

    private bool isGrounded;
    private MovementState movementState = MovementState.Idle;
    private bool isShooting = false;
    private float shootCooldown = 0.5f; // Anpassbare Verzögerung zwischen den Shoot-Animationen
    private float shootCooldownTimer = 0f;
    private Transform firePoint;

    private bool canShoot = true; // Eingabe-Verzögerung
    private float shootInputCooldown = 0.2f; // Abstand zwischen Schüssen

    private enum MovementState
    {
        Idle,
        Running,
        Jumping,
        Shooting
    }

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        groundCheck = transform.Find("GroundCheck");
        firePoint = transform.Find("FirePoint");
        weapon = GetComponentInChildren<Weapon>(); // Finde das Weapon-Skript im Kindobjekt
    }

    private void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);

        // Bewegung des Spielers
        Vector2 movement = new Vector2(horizontalInput, 0f);
        rb.velocity = new Vector2(movement.x * moveSpeed, rb.velocity.y);

        // Ändere die Animation basierend auf der horizontalen Bewegung und dem Zustand
        UpdateAnimationState(horizontalInput);

        // Springen
        if (isGrounded && Input.GetButtonDown("Jump"))
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            SetMovementState(MovementState.Jumping);
        }

        // Schießen
        if (Input.GetButtonDown("Fire1") && weapon != null && !isShooting)
        {
            if (canShoot)
            {
                Shoot();
                canShoot = false;
                StartCoroutine(EnableShootingAfterDelay(shootInputCooldown));
            }
        }

        // Schuss-Cooldown aktualisieren
        if (isShooting)
        {
            shootCooldownTimer -= Time.deltaTime;
            if (shootCooldownTimer <= 0f)
            {
                isShooting = false;
            }
        }
    }

    private void UpdateAnimationState(float horizontalInput)
    {
        if (Mathf.Abs(horizontalInput) > 0.1f)
        {
            SetMovementState(MovementState.Running);

            if (horizontalInput < 0f)
            {
                transform.localScale = new Vector3(-1f, 1f, 1f);
            }
            else
            {
                transform.localScale = new Vector3(1f, 1f, 1f);
            }
        }
        else
        {
            if (!isShooting) // Nur in den Idle-Modus wechseln, wenn nicht geschossen wird
            {
                SetMovementState(MovementState.Idle);
            }
        }
    }

    private void SetMovementState(MovementState state)
    {
        if (state != movementState)
        {
            movementState = state;
            animator.SetInteger("MovementState", (int)state);
        }
    }

    private void Shoot()
    {
        SetMovementState(MovementState.Shooting);
        isShooting = true;
        shootCooldownTimer = shootCooldown;
        weapon.Shoot(); // Schieße mit der Waffe
    }

    // Coroutine, um das Schießen nach einer Verzögerung zu aktivieren
    private IEnumerator EnableShootingAfterDelay(float delay)
    {
        yield return new WaitForSeconds(delay);
        canShoot = true;
    }
}

dear god does no one ever format their code

I started to learn Unity 3 weeks ago and this is my first game. At the moment i*m happy when everything works. No idea about formatting the right way.

Its not your unity code necessarily - its your post here!!!

I dont understand. Whats wrong with my post?

yoru code like that is unreadable

for example, heres a chunk of code

    void Start()
    {
        controls = new NewControls();
       
       // controls.Newactionmap.Newaction.performed += ctx => Debug.Log("Move start");
       // controls.Newactionmap.Newaction.canceled += ctx => Debug.Log("Move Stop");
        controls.Newactionmap.act1.performed += ctx => Debug.Log("act1 start");
        controls.Newactionmap.act1.canceled += ctx => Debug.Log("act1 Stop");
        controls.Enable();
    }

see how more readable it is?

so go back edit the post with your code in, and copy it, click on the very large “code” button on the bar above where you type, and paste it in there… it will then format it, and colour code it, and give it line numbers and keep the indents and everything… then we can see what you wrote…

Thank you. I changed it.

you have 2 shooting cooldown systems

dont know why you did that

but probably its happening that the coroutine is finishing your cooldown just in the moment that you started the update cooldown

delete either the coroutine or the cooldown timer on the update

make only one logic system to control the cooldown

Thank you soo much! Works perfectly now. Tested different cooldows and forgot to delete one.

1 Like