private void Fire() { if (justFired) { return; } OnProjectileFired?.Invoke(); var projectile = new GameObject(); projectile.name = "Projectile"; projectile.transform.position = gameObject.transform.position; projectile.transform.rotation = gameObject.transform.rotation; projectile.transform.SetParent(ProjectileContainer.transform); var spriteRenderer = projectile.AddComponent <SpriteRenderer>(); spriteRenderer.sprite = CurrentWeapon.image; var coll = projectile.AddComponent <BoxCollider2D>(); coll.isTrigger = true; projectile.AddComponent <DestroyWhenOffScreen>(); projectile.AddComponent <Projectile>(); var rb = projectile.AddComponent <Rigidbody2D>(); rb.bodyType = RigidbodyType2D.Kinematic; rb.velocity = transform.up * CurrentWeapon.speed; justFired = true; timeSinceFired = 0f; }
private void HandleAttack() { if (Time.time > _nextAttackTime) { SpawnProjectiles(); OnProjectileFired?.Invoke(); _nextAttackTime = Time.time + _stats.GetStatValue(StatType.AttackRatio); _animator.SetBool("IsAttacking", true); } }
public void FireProjectile() { gun.transform.forward = (target.transform.position - transform.position).normalized; Vector3 adjTargetPos = target.transform.position; adjTargetPos.y = 1.5f; Quaternion rot = Quaternion.LookRotation(adjTargetPos - projectileSpawnPoints[_nextGunToShoot].position); Projectile prj = Instantiate <GameObject>(projectilePrefab, projectileSpawnPoints[_nextGunToShoot].position, rot).GetComponent <Projectile>(); Instantiate <GameObject>(muzzleFlashVfx, projectileSpawnPoints[_nextGunToShoot].position, rot); prj.SetTarget(target); prj.damage = damage; _nextGunToShoot = (_nextGunToShoot + 1) % projectileSpawnPoints.Length; OnProjectileFired?.Invoke(this); }
private void FireProjectile(int index) { Transform muzzle = Muzzles[index % Muzzles.Length]; int amount = Mathf.RoundToInt(Amount); for (int i = 0; i < amount; i++) { GameObject newProjectile = Instantiate(Projectile, muzzle.position, muzzle.rotation); Projectile projectileScript = newProjectile.GetComponent <Projectile>(); float rad = Pricision * Mathf.Deg2Rad; Vector2 direction = muzzle.right + muzzle.rotation * (Vector3.up * Mathf.Sin(UnityEngine.Random.Range(-rad, rad))); projectileScript.Fire(this, direction, LayerMask, Target, Damage, Range); AttachProjectileCallbacks(projectileScript); OnProjectileFired?.Invoke(new ProjectileEventInfo(projectileScript)); } OnFire?.Invoke(new FireEventInfo(muzzle, this)); }
public void LookAheadCollision(RaycastHit2D hit) { if (collisionState == CollisionState.MovingToCollision) { return; } bool projectileFired = projectileManager.ShootProjectile(hit); OnProjectileFired?.Invoke(projectileFired); if (projectileFired) { return; } collisionState = CollisionState.MovingToCollision; startTween.Kill(); GameManager.StartEndGame(); transform.DOMoveY(hit.collider.transform.position.y, 1f) .SetEase(Ease.InOutSine); }