protected virtual void HandleShooting() { if (bulletPrefab == null) { return; } if (firingTimer <= 0) { // Fire the shot. Vector2 shootDir = (target.position - transform.position).normalized; Vector2 spawnPos = rigidbody.position + shootDir; Bullet2D bullet = Instantiate <Bullet2D>(bulletPrefab, spawnPos, Quaternion.identity); // Make sure the bullet moves in the right direction, and it on the same layer as // this enemy. bullet.velocity = shootDir; bullet.gameObject.layer = this.gameObject.layer; firingTimer = fireRate; } else { firingTimer -= Time.deltaTime; } }
protected virtual void HandleShooting() { if (firingTimer <= 0) { canShoot = true; } else { firingTimer -= Time.deltaTime; } if (canShoot && Input.GetButton("Fire1")) { // Spawn the bullet at the right position. float bulletOffset = 1; Vector2 spawnPos = rigidbody.position + (Vector2.up * bulletOffset); Bullet2D bullet = Instantiate <Bullet2D>(bulletPrefab, spawnPos, Quaternion.identity); // Make sure the bullet is on the same layer as this, and it moves in the right // direction. bullet.gameObject.layer = this.gameObject.layer; Vector3 shotDir = spawnPos - rigidbody.position; bullet.velocity = shotDir * bullet.moveSpeed; firingTimer = 1 / fireRate; canShoot = false; // ^Only allow shooting when the timer is up again. } }
/// <summary> /// Returns true if successful, false otherwise. /// </summary> public virtual bool Shoot(Vector2 direction) { if (fireTimer <= 0) { // Instantiate the bullet wherever this is, and have it move in the direction of the shotForce. Bullet2D bullet = Instantiate <Bullet2D>(bulletPrefab, transform.position, Quaternion.identity); bullet.gameObject.layer = LayerMask.NameToLayer(bulletLayer); bullet.velocity = shotForce * direction; // Set the timer for the delay between this and the next shot. fireTimer = 1 / fireRate; return(true); } return(false); }
void FanShot() { // Decide where each bullet should go. float distOffset = 1; List <Vector2> bulletPositions = new List <Vector2>(); Vector2 bulletPos = Vector2.zero; float // The min and max angles decide the shape and size of the fan. minAngle = 200, maxAngle = 340, angleStep = ((maxAngle - minAngle) / (bulletsPerFan - 1)) * Mathf.Deg2Rad, shotAngle = minAngle * Mathf.Deg2Rad; // angleStep is to help the bullets be spread as evenly as possible in the fan. for (int i = 0; i < bulletsPerFan; i++) { bulletPos.x = Mathf.Cos(shotAngle) * distOffset; bulletPos.y = Mathf.Sin(shotAngle) * distOffset; bulletPos += rigidbody.position; bulletPositions.Add(bulletPos); shotAngle += angleStep; } // Now, spawn bullets at each of those positions... Vector2 shotDir = Vector2.zero; foreach (Vector2 spawnPos in bulletPositions) { Bullet2D bullet = Instantiate <Bullet2D>(bulletPrefab, spawnPos, Quaternion.identity); // ... Making them go off in the right directions. shotDir = bullet.rigidbody.position - rigidbody.position; bullet.velocity = shotDir.normalized * bullet.moveSpeed; } }
public void Fire(Vector3 direction) { // Create bullet prefab, have the bullet move in the given direction if (canShoot) { //player.animator.SetTrigger("Shoot"); //player.animator.SetBool("Shooting", true); Vector3 originPos = Vector3.zero; //Make sure that when shooting up, the bullet starts above the player. if (direction == Vector3.up) { originPos = user.transform.position; } else { originPos = transform.position; } Vector3 spawnPos = originPos + (direction.normalized * shotDist); GameObject bulletGO = Instantiate <GameObject>(bulletPrefab.gameObject, spawnPos, Quaternion.identity); Bullet2D bullet = bulletGO.GetComponent <Bullet2D>(); bullet.direction = direction; float tilt = 0; int yDirection = 0; // Used for the way the gun can shoot diagonally if (Input.GetAxis("Vertical") == 0) { tilt = 0; } else if (Input.GetAxis("Vertical") > 0) { tilt = 0.3f; yDirection = 1; } else if (Input.GetAxis("Vertical") < 0) { tilt = 0.3f; yDirection = -1; } if (direction == Vector3.right) { bullet.velX = Mathf.Sqrt(Mathf.Pow(bullet.speed, 2) * (1 - Mathf.Abs(tilt))) * 1; bullet.vely = Mathf.Sqrt(Mathf.Pow(bullet.speed, 2) * Mathf.Abs(tilt)) * yDirection; } else if (direction == Vector3.left) { bullet.velX = Mathf.Sqrt(Mathf.Pow(bullet.speed, 2) * (1 - Mathf.Abs(tilt))) * -1; bullet.vely = Mathf.Sqrt(Mathf.Pow(bullet.speed, 2) * Mathf.Abs(tilt)) * yDirection; } else if (direction == Vector3.up) { bullet.vely = Mathf.Sqrt(Mathf.Pow(bullet.speed, 2) * Mathf.Abs(tilt)) * yDirection; bullet.velX = 0; } // Set the timer and disable shooting timer = 1 / shotsPerSecond; canShoot = false; // Put the bullet on the right layer so it doesn't collide with whatever/whoever // shot it //bulletGO.layer = gameObject.layer;// Just set the physics dude } }