private void Update() { if (!bulletModel.IsAlive(Time.deltaTime)) //bullet expired { if (bulletModel.GetExplosive().amount != 0) //is explosive { var warhead = bulletModel.GetExplosive(); var bulletPrefab = Resources.Load <GameObject>("Prefabs/Bullet"); for (int i = 0; i < warhead.amount; i++) { float angle = i * 2 * Mathf.PI / warhead.amount; Bullet.Direction direction; direction.x = Mathf.Cos(angle); direction.y = Mathf.Sin(angle); Bullet bulletModel = new Bullet(TurretController.BulletReference[warhead.bulletName], direction); GameObject bulletGO = Instantiate(bulletPrefab, transform.position, Quaternion.identity); BulletBehavior bulletBehavior = bulletGO.GetComponent <BulletBehavior>(); bulletBehavior.SetModel(bulletModel); } } Destroy(gameObject); } if (bulletModel.GetExplosive().amount != 0) { animator.SetFloat("TimeLeft", animator.GetFloat("TimeLeft") - Time.deltaTime); } if (!bulletModel.MoveActive) { moveType = Bullet.Move.Straight; } else { moveType = bulletModel.MoveType; } switch (moveType) { case Bullet.Move.Curve: bulletModel.UpdateDirection(Time.deltaTime); break; case Bullet.Move.Home: Vector3 relativePosition = player.position - transform.position; relativePosition.Normalize(); bulletModel.UpdateDirection(Time.deltaTime, ConvertVector(relativePosition)); break; default: break; } moveDirection = ConvertVector(bulletModel.UpdateDirection()); rigidBody.velocity = moveDirection * bulletModel.Speed; }
private void Fire(Vector2 vector, Vector2 position, string bullet) { Bullet.Direction direction; direction.x = vector.x; direction.y = vector.y; Bullet bulletModel = new Bullet(BulletReference[bullet], direction); GameObject bulletGO = Instantiate(bulletPrefab, position, Quaternion.identity); BulletBehavior bulletBehavior = bulletGO.GetComponent <BulletBehavior>(); bulletBehavior.SetModel(bulletModel); }