public void Attack() { if (CanAttack) { shootingRate = Random.Range(2f, 6f); shootCooldown = shootingRate; enemyWeapon.Shoot(); } }
public void Attack() { _currentRateAttack += Time.deltaTime; if (_currentRateAttack >= rateAttack) { _currentRateAttack = 0; weapon.Shoot(); } }
public void UpdateState() { // this is a shooting state // so we are going to pass the shoot to a script based in the enemy weapon // this is useful to change the weapon script of the enemy independently of this script enemy.agent.enabled = false; // stop moving // is the coolDown ? //check Range if (weapon) { RaycastHit hit; Vector3 directionTarget = enemy.target.transform.position - enemy.transform.position; if (weapon != null && weapon is EnemyMeleeWeapon) { //melee attack doesnt need direct vision weapon.PrepareShoot(); weapon.Shoot(); } else { // the range attack requires direct shoot vision // Debug.DrawRay(enemy.transform.position, enemy.transform.TransformDirection(directionTarget), Color.red); if (Physics.Raycast(enemy.transform.position, enemy.transform.TransformDirection(directionTarget), out hit, Mathf.Infinity)) { weapon.target = enemy.target.transform; //is direct range, shoot weapon.PrepareShoot(); weapon.Shoot(); } } } // after shoot check if the enemy has change state if (enemy.actualDistance > enemy.followRange) { ToPatrolState(); } else if (enemy.actualDistance < enemy.shootRange) { ToFollowState(); //alternates between follow and shoot } }
void Update() { float xx = speed; float yy = 0; if (goingLeft) { xx *= -1; } if (transform.position.y > destY) { yy = -speed; } rb2d.velocity = new Vector2(xx * moveSpeed, yy * moveSpeed); if (transform.position.x <= -3.2f + leftOffset && goingLeft || transform.position.x >= 3.2f - rightOffset && !goingLeft) { goingLeft = !goingLeft; } /* speedup by time */ if (speed < maxSpeed) // speeding up { ticks += 1; if (ticks >= tickToSpeedup) { SpeedUp(); ticks = 0; } } /* */ // shooting if (canShoot) { if (ticksToShoot > 0) { ticksToShoot -= 1; } else if (ticksToShoot <= 0 && Random.Range(0, 100) < 20) { if (maxTicksToShoot > minTicksToShoot) { maxTicksToShoot -= 1; } ticksToShoot = maxTicksToShoot; if (transform.position.y <= 2.5f) { weapon.Shoot(level); } } } // -shooting }
public override void Tick() { LookAtTarget(); if (!_enemyWeapon.CanShoot()) { return; } _enemyWeapon.Shoot(Mover.FacingRight); if (_hasAnimator) { _animator.SetTrigger(Shoot); } }
IEnumerator shoot() { while (true) { yield return(new WaitForSeconds(fireCooldown)); for (int i = 0; i < shotsPerRound; i++) { weapon.Shoot(getShotDirection()); yield return(new WaitForSeconds(roundInterval)); } } }
protected override void PerformAttack() { base.PerformAttack(); Vector3 angle = (f.p.transform.position + Vector3.up) - arm.transform.position; Quaternion rot = Quaternion.LookRotation(angle); arm.transform.rotation = rot; arm.transform.eulerAngles = new Vector3(0, arm.transform.eulerAngles.y + 90, arm.transform.eulerAngles.z - 85); if (Random.Range(0, 5) > 3) { arm.transform.eulerAngles = new Vector3(0, arm.transform.eulerAngles.y + 10, arm.transform.eulerAngles.z + 10); } myGun.Shoot(); }
void attack() { //---------------------------------------------------------------- checkFlip(); if (Vector2.Distance(transform.position, player.transform.position) >= weaponRange) //merg spre player { animator.SetBool("aiming", false); enemyWeapon.gameObject.SetActive(false); if (!coroutineStarted) { StartCoroutine(UpdatePlayerPos()); } transform.position = Vector3.MoveTowards(transform.position, playerPos, enemySpeed * Time.deltaTime); lastAttackTime = Time.time; } //---------------------------------------------------------------- else if (Vector2.Distance(transform.position, player.transform.position) <= weaponRange) //daca playerul e in range, atac cu pistolul { animator.SetBool("aiming", true); enemyWeapon.gameObject.SetActive(true); if (Time.time > lastAttackTime + ((RNG.Rng() % 2) + 1 * 1.0) * enemyWeapon.fireRate) { float dir; if (facingRight) { dir = 1; } else { dir = -1; } enemyWeapon.Shoot(dir); lastAttackTime = Time.time; } } }
void Update() { var playerDirection = (character.transform.position - transform.position).normalized; switch (state) { case Patrolling: if (patrolTimer <= 0) { patrolTimer = patrolPeriod; // Change patrolDirection rand = Random.value; } patrolTimer -= Time.deltaTime; // Move enemy if (rand < .5f) { if (enemy.stuck) { if (enemy.ceiling) { enemy.Flip(); rand = 0.51f; } else { enemy.jump(); } } else { enemy.moveLeft(); } } else { if (enemy.stuck) { if (enemy.ceiling) { enemy.Flip(); rand = 0.49f; } else { enemy.jump(); } } else { enemy.moveRight(); } } distance = Vector3.Distance(transform.position, character.transform.position); if (distance <= detectionDistance) { state = Attacking; } // ------------------------------- break; case Attacking: if (pHealth.dead == true) { state = Patrolling; } else { enemy.stopMoving(); float dot = Vector3.Dot((character.transform.position - transform.position).normalized, transform.right); if (dot > 0) { if (!enemy.isFacingRight()) { enemy.Flip(); } } if (dot <= 0) { if (enemy.isFacingRight()) { enemy.Flip(); } } enemyArm.rotateArm(); if (gunCooldown <= 0) { gunCooldown = gunReloadTime; // Fire gun at player RaycastHit2D hit = Physics2D.Raycast(transform.position, character.transform.position - transform.position, Mathf.Infinity, whatToHit); if (hit.collider.gameObject.tag == "tile") { return; } else if (hit.collider.gameObject.tag == "Player") { enemyWeapon.Shoot(); } } gunCooldown -= Time.deltaTime; distance = Vector3.Distance(transform.position, character.transform.position); if (distance >= followDistance) { state = Following; } } break; case Following: if (pHealth.dead == true) { state = Patrolling; } else { float dot2 = Vector3.Dot((character.transform.position - transform.position).normalized, transform.right); // Character on right if (dot2 > 0) { if (enemy.stuck) { if (enemy.ceiling) { enemy.Flip(); dot2 = -1f; } else { enemy.jump(); } } else { enemy.moveRight(); } } // Character on left if (dot2 <= 0) { if (enemy.stuck) { if (enemy.ceiling) { enemy.Flip(); dot2 = 1f; } else { enemy.jump(); } } else { enemy.moveLeft(); } } enemyArm.rotateArm(); if (gunCooldown <= 0) { gunCooldown = gunReloadTime; RaycastHit2D hit = Physics2D.Raycast(transform.position, character.transform.position - transform.position, Mathf.Infinity, whatToHit); if (hit.collider.gameObject.tag == "tile") { return; } else if (hit.collider.gameObject.tag == "Player") { enemyWeapon.Shoot(); } } gunCooldown -= Time.deltaTime; distance = Vector3.Distance(transform.position, character.transform.position); if (distance >= detectionDistance) { state = Patrolling; } if (distance < followDistance) { state = Attacking; } } break; } }