When I hit spacebar multiple times immediately after clicking the play button, spaceship keeps firing even if I stop pressing spacebar:-
But if I wait for like one or half second after clicking the play button and then hit the spacebar, everything works fine as it should be:-
Don’t know what I am doing wrong.
Player.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
//Configuration Parameter
[Header("Player")]
[SerializeField] float moveSpeed = 10f;
[SerializeField] float padding = 0.4f;
[SerializeField] float Ypadding = 3.5f;
[SerializeField] float Health = 200;
[SerializeField] AudioClip DeathSFX;
[SerializeField] [Range(0, 1)] float DeathSFXVolume = 0.75f;
[Header("Projectile")]
[SerializeField] GameObject LaserPrefab_Player;//To attach "Laser" prefab to "Player" gameobject
[SerializeField] float ProjectileSpeed_Player = 10f;
[SerializeField] float offSet_LaserPos_R_x = 0.15f;
[SerializeField] float offSet_LaserPos_R_y = 0.15f;
[SerializeField] float offSet_LaserPos_L_x = -0.15f;
[SerializeField] float offSet_LaserPos_L_y = 0.15f;
[SerializeField] float ProjectileFiringPeriod_Player =0.1f;
[SerializeField] AudioClip ProjectileSFX;
[SerializeField] [Range(0, 1)] float ProjectileSFXVolume = 0.75f;
float xMin, xMax,yMin,yMax;
Vector3 LaserPos_R;
Vector3 LaserPos_L;
Coroutine FiringCoroutine;
//Use this for initialisations
void Start ()
{
SetUpMoveBoundaries();
}
void Update ()
{
Move();
Fire();
}
private void Fire()
{
LaserPos_R = transform.position;
LaserPos_R.x += offSet_LaserPos_R_x;
LaserPos_R.y += offSet_LaserPos_R_y;
LaserPos_L = transform.position;
LaserPos_L.x += offSet_LaserPos_L_x;
LaserPos_L.y += offSet_LaserPos_L_y;
if (Input.GetButtonDown("Fire1"))
{
FiringCoroutine=StartCoroutine(FireContinuously());
}
else if (Input.GetButtonUp("Fire1"))
{
StopCoroutine(FiringCoroutine);
}
}
IEnumerator FireContinuously()
{
while (true)
{
GameObject laser_R = Instantiate(LaserPrefab_Player, LaserPos_R, Quaternion.identity) as GameObject;
laser_R.GetComponent<Rigidbody2D>().velocity = new Vector2(0, ProjectileSpeed_Player);
GameObject laser_L = Instantiate(LaserPrefab_Player, LaserPos_L, Quaternion.identity) as GameObject;
laser_L.GetComponent<Rigidbody2D>().velocity = new Vector2(0, ProjectileSpeed_Player);
AudioSource.PlayClipAtPoint(ProjectileSFX, Camera.main.transform.position, ProjectileSFXVolume);
yield return new WaitForSeconds(ProjectileFiringPeriod_Player);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
if (!damageDealer) { return; }
ProcessHit(damageDealer);
}
private void ProcessHit(DamageDealer damageDealer)
{
Health -= damageDealer.GetDamage();
damageDealer.Hit();
if (Health <= 0)
{
Die();
}
}
private void Die()
{
Destroy(gameObject);
AudioSource.PlayClipAtPoint(DeathSFX, Camera.main.transform.position, DeathSFXVolume);
}
private void Move()
{
var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;//Change what player is going to do in y axis
var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;//Change what player is going to do in X axis
var newYpos = Mathf.Clamp(transform.position.y + deltaY,yMin,yMax);//Position that we want ship to go in y axis
var newXPos = Mathf.Clamp(transform.position.x + deltaX,xMin,xMax);//Position that we want ship to go in x axis
transform.position = new Vector2(newXPos, newYpos);//will make ship position change
}
private void SetUpMoveBoundaries()
{
Camera gameCamera = Camera.main;
xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding;
xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding;
yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding;
yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - Ypadding;
}
}