Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Why is my firepoint sometimes spawning 2 projectiles?

Discussion in '2D' started by Feuerkopf65, Aug 27, 2023.

  1. Feuerkopf65

    Feuerkopf65

    Joined:
    Aug 6, 2023
    Posts:
    7
  2. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    142
    you coded it incorrectly
     
    bugfinders likes this.
  3. Feuerkopf65

    Feuerkopf65

    Joined:
    Aug 6, 2023
    Posts:
    7
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     private Rigidbody2D rb;
    8.     private Animator animator;
    9.     private Weapon weapon; // Referenz auf das Weapon-Skript
    10.  
    11.     [SerializeField] private float moveSpeed = 5f;
    12.     [SerializeField] private float jumpForce = 10f;
    13.     [SerializeField] private Transform groundCheck;
    14.     [SerializeField] private LayerMask groundLayer;
    15.  
    16.     private bool isGrounded;
    17.     private MovementState movementState = MovementState.Idle;
    18.     private bool isShooting = false;
    19.     private float shootCooldown = 0.5f; // Anpassbare Verzögerung zwischen den Shoot-Animationen
    20.     private float shootCooldownTimer = 0f;
    21.     private Transform firePoint;
    22.  
    23.     private bool canShoot = true; // Eingabe-Verzögerung
    24.     private float shootInputCooldown = 0.2f; // Abstand zwischen Schüssen
    25.  
    26.     private enum MovementState
    27.     {
    28.         Idle,
    29.         Running,
    30.         Jumping,
    31.         Shooting
    32.     }
    33.  
    34.     private void Start()
    35.     {
    36.         rb = GetComponent<Rigidbody2D>();
    37.         animator = GetComponent<Animator>();
    38.         groundCheck = transform.Find("GroundCheck");
    39.         firePoint = transform.Find("FirePoint");
    40.         weapon = GetComponentInChildren<Weapon>(); // Finde das Weapon-Skript im Kindobjekt
    41.     }
    42.  
    43.     private void Update()
    44.     {
    45.         float horizontalInput = Input.GetAxis("Horizontal");
    46.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
    47.  
    48.         // Bewegung des Spielers
    49.         Vector2 movement = new Vector2(horizontalInput, 0f);
    50.         rb.velocity = new Vector2(movement.x * moveSpeed, rb.velocity.y);
    51.  
    52.         // Ändere die Animation basierend auf der horizontalen Bewegung und dem Zustand
    53.         UpdateAnimationState(horizontalInput);
    54.  
    55.         // Springen
    56.         if (isGrounded && Input.GetButtonDown("Jump"))
    57.         {
    58.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    59.             SetMovementState(MovementState.Jumping);
    60.         }
    61.  
    62.         // Schießen
    63.         if (Input.GetButtonDown("Fire1") && weapon != null && !isShooting)
    64.         {
    65.             if (canShoot)
    66.             {
    67.                 Shoot();
    68.                 canShoot = false;
    69.                 StartCoroutine(EnableShootingAfterDelay(shootInputCooldown));
    70.             }
    71.         }
    72.  
    73.         // Schuss-Cooldown aktualisieren
    74.         if (isShooting)
    75.         {
    76.             shootCooldownTimer -= Time.deltaTime;
    77.             if (shootCooldownTimer <= 0f)
    78.             {
    79.                 isShooting = false;
    80.             }
    81.         }
    82.     }
    83.  
    84.     private void UpdateAnimationState(float horizontalInput)
    85.     {
    86.         if (Mathf.Abs(horizontalInput) > 0.1f)
    87.         {
    88.             SetMovementState(MovementState.Running);
    89.  
    90.             if (horizontalInput < 0f)
    91.             {
    92.                 transform.localScale = new Vector3(-1f, 1f, 1f);
    93.             }
    94.             else
    95.             {
    96.                 transform.localScale = new Vector3(1f, 1f, 1f);
    97.             }
    98.         }
    99.         else
    100.         {
    101.             if (!isShooting) // Nur in den Idle-Modus wechseln, wenn nicht geschossen wird
    102.             {
    103.                 SetMovementState(MovementState.Idle);
    104.             }
    105.         }
    106.     }
    107.  
    108.     private void SetMovementState(MovementState state)
    109.     {
    110.         if (state != movementState)
    111.         {
    112.             movementState = state;
    113.             animator.SetInteger("MovementState", (int)state);
    114.         }
    115.     }
    116.  
    117.     private void Shoot()
    118.     {
    119.         SetMovementState(MovementState.Shooting);
    120.         isShooting = true;
    121.         shootCooldownTimer = shootCooldown;
    122.         weapon.Shoot(); // Schieße mit der Waffe
    123.     }
    124.  
    125.     // Coroutine, um das Schießen nach einer Verzögerung zu aktivieren
    126.     private IEnumerator EnableShootingAfterDelay(float delay)
    127.     {
    128.         yield return new WaitForSeconds(delay);
    129.         canShoot = true;
    130.     }
    131. }
     
    Last edited: Aug 27, 2023
  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    dear god does no one ever format their code
     
  5. Feuerkopf65

    Feuerkopf65

    Joined:
    Aug 6, 2023
    Posts:
    7
    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.
     
  6. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    Its not your unity code necessarily - its your post here!!!
     
  7. Feuerkopf65

    Feuerkopf65

    Joined:
    Aug 6, 2023
    Posts:
    7
    I don`t understand. What`s wrong with my post?
     
  8. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    yoru code like that is unreadable

    for example, heres a chunk of code

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         controls = new NewControls();
    4.        
    5.        // controls.Newactionmap.Newaction.performed += ctx => Debug.Log("Move start");
    6.        // controls.Newactionmap.Newaction.canceled += ctx => Debug.Log("Move Stop");
    7.         controls.Newactionmap.act1.performed += ctx => Debug.Log("act1 start");
    8.         controls.Newactionmap.act1.canceled += ctx => Debug.Log("act1 Stop");
    9.         controls.Enable();
    10.     }
    see how more readable it is?
     
  9. Feuerkopf65

    Feuerkopf65

    Joined:
    Aug 6, 2023
    Posts:
    7
     
  10. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    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..

    upload_2023-8-27_16-24-48.png
     
  11. Feuerkopf65

    Feuerkopf65

    Joined:
    Aug 6, 2023
    Posts:
    7
    Thank you. I changed it.
     
  12. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    142
    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
     
  13. Feuerkopf65

    Feuerkopf65

    Joined:
    Aug 6, 2023
    Posts:
    7
    Thank you soo much! Works perfectly now. Tested different cooldows and forgot to delete one.
     
    sildeflask likes this.