void handleShoot() { if (shootyPoint == null) { return; } Vector2 shootDir = getShoot(); if (Time.fixedTime - lastShot >= bulletCooldown && (autoFire || shootDir.magnitude < ggd.accuracyLimit)) { canShoot = true; } if (canShoot && bulletTemplate != null && shootDir.magnitude > ggd.accuracyLimit) { canShoot = false; lastShot = Time.fixedTime; GameObject go = GameObject.Instantiate(bulletTemplate.gameObject); BulletBehavior created = go.GetComponent <BulletBehavior>(); if (shootDir.x < 0) { created.initVel = shootDir * shootyForce; created.transform.position = new Vector2(transform.position.x - shootyPoint.localPosition.x, transform.position.y); go.GetComponent <SpriteRenderer>().flipX = true; } else { created.initVel = shootDir * shootyForce; created.transform.position = shootyPoint.position; } created.GetComponent <Rigidbody2D>().velocity = rb2.velocity; audioManager.playClipHere("shoot", transform.position); } }
void Shoot(Collider2D target) { GameObject bulletPrefab = monsterData.CurrentLevel.bullet; Vector3 startPosition = gameObject.transform.position; Vector3 targetPosition = target.transform.position; startPosition.z = bulletPrefab.transform.position.z; targetPosition.z = bulletPrefab.transform.position.z; GameObject newBullet = (GameObject)Instantiate(bulletPrefab); newBullet.transform.position = startPosition; BulletBehavior bulletComp = newBullet.GetComponent <BulletBehavior>(); bulletComp.target = target.gameObject; bulletComp.startPosition = startPosition; bulletComp.targetPosition = targetPosition; Animator animator = monsterData.CurrentLevel.visualization.GetComponent <Animator>(); animator.SetTrigger("fireShot"); AudioSource audioSource = gameObject.GetComponent <AudioSource>(); audioSource.PlayOneShot(audioSource.clip); }
void Shoot(Collider2D target) { GameObject bulletPrefab = animalData.CurrentLevel.bullet; // Get the start and target positions of the bullet. // Set the z-Position to that of bulletPrefab. Vector3 startPosition = gameObject.transform.position; Vector3 targetPosition = target.transform.position; startPosition.z = bulletPrefab.transform.position.z; targetPosition.z = bulletPrefab.transform.position.z; // Instantiate a new bullet using the bulletPrefab for MonsterLevel. // Assign the startPosition and targetPosition of the bullet. GameObject newBullet = (GameObject)Instantiate(bulletPrefab); newBullet.transform.position = startPosition; BulletBehavior bulletComp = newBullet.GetComponent <BulletBehavior>(); bulletComp.target = target.gameObject; bulletComp.startPosition = startPosition; bulletComp.targetPosition = targetPosition; AudioSource audioSource = gameObject.GetComponent <AudioSource>(); audioSource.PlayOneShot(audioSource.clip); }
void OnTriggerEnter2D(Collider2D other) { BulletBehavior bullet = other.GetComponent <BulletBehavior>(); health -= bullet.damage; if (health <= 0) { foreach (Astroid.AstroidLoot loot in astroidInfo.loots) { float ran = Random.Range(0f, 100f); if (ran <= loot.chance) { GameObject itemDrop = Instantiate(pickUpPrefab); itemDrop.transform.position = transform.position; itemDrop.GetComponent <pickUp>().itemToPickUp = loot.itemToSpawn; } } Destroy(gameObject); } else { render.sprite = astroidInfo.sprites[astroidInfo.sprites.Length - Mathf.CeilToInt(health / astroidDestroyedFactor)]; } Destroy(other.gameObject); }
void Shoot(Collider2D target) { GameObject bulletPrefab = monsterData.CurrentLevel.bullet; //Get the start and target positions of the bullet. Set the z-Position to that of bulletPrefab. //Earlier, you set the bullet prefab’s z position value to make sure the bullet appears behind the monster firing it, but in front of the enemies. Vector3 startPosition = gameObject.transform.position; Vector3 targetPosition = target.transform.position; startPosition.z = bulletPrefab.transform.position.z; targetPosition.z = bulletPrefab.transform.position.z; //Instantiate a new bullet using the bulletPrefab for MonsterLevel. Assign the startPosition and targetPosition of the bullet. GameObject newBullet = (GameObject)Instantiate(bulletPrefab); newBullet.transform.position = startPosition; BulletBehavior bulletComp = newBullet.GetComponent <BulletBehavior>(); bulletComp.target = target.gameObject; bulletComp.startPosition = startPosition; bulletComp.targetPosition = targetPosition; //Run a shoot animation and play a laser sound whenever the monster shoots. Animator animator = monsterData.CurrentLevel.visualization.GetComponent <Animator>(); animator.SetTrigger("fireShot"); AudioSource audioSource = gameObject.GetComponent <AudioSource>(); audioSource.PlayOneShot(audioSource.clip); }
void Shoot(Collider2D target) { GameObject bulletPrefab = monsterData.CurrentLevel.bullet; GameObject bullet = Instantiate(bulletPrefab, gameObject.transform.position, Quaternion.identity); // Init bullet behavior value BulletBehavior bulletBehavior = bullet.GetComponent <BulletBehavior>(); bulletBehavior.target = target.gameObject; bulletBehavior.startPosition = gameObject.transform.position; bulletBehavior.targetPosition = target.transform.position; // reset z value bulletBehavior.startPosition.z = bullet.transform.position.z; bulletBehavior.targetPosition.z = bullet.transform.position.z; // animation Animator animator = monsterData.CurrentLevel.visualization.GetComponent <Animator>(); animator.SetTrigger("fireShot"); // sound play AudioSource audioSource = gameObject.GetComponent <AudioSource>(); audioSource.PlayOneShot(audioSource.clip); }
void Shoot(CircleCollider2D target) { GameObject bulletPrefab = monsterData.CurrentLevel.bullet; //赋与子弹 // 1 Vector3 startPosition = gameObject.transform.position; Vector3 targetPosition = target.transform.position; startPosition.z = bulletPrefab.transform.position.z; targetPosition.z = bulletPrefab.transform.position.z; // 2 GameObject newBullet = (GameObject)Instantiate(bulletPrefab, transform.position, Quaternion.identity); //创建子弹 newBullet.transform.parent = transform.parent; newBullet.transform.localScale = Vector3.one; //newBullet.transform.position = startPosition; // newBullet.transform.rotation = Quaternion.Euler(0, 0, 0); //newBullet.transform.rotation = Quaternion.Euler(0, 0, transform.position.z + 45f); BulletBehavior bulletComp = newBullet.GetComponent <BulletBehavior>();//得到子弹脚本 bulletComp.target = target.transform.gameObject; bulletComp.startPosition = startPosition; bulletComp.targetPosition = targetPosition; //print(targetPosition); // 3 //Animator animator = monsterData.CurrentLevel.visualization.GetComponent<Animator> (); //animator.SetTrigger ("fireShot"); //AudioSource audioSource = gameObject.GetComponent<AudioSource>(); //audioSource.PlayOneShot(audioSource.clip); }
/** * This script will first get a collider and first decide if it will harm them. * In the case if it does, hp will be subtracted from the projectile's damage. * When hp reaches 0 or less, the object this script is attached to will die. */ private void OnTriggerEnter2D(Collider2D collision) { BulletBehavior projectile = collision.gameObject.GetComponent <BulletBehavior>(); HealthScript colliderHealth = collision.gameObject.GetComponent <HealthScript>(); print(gameObject.name + " collided with " + collision.gameObject.name); if (projectile != null) { if (projectile.isEnemyShot != isEnemy) { print(gameObject.name + " was just hit for " + projectile.dmg + " damage!"); current_hp -= projectile.dmg; print(gameObject.name + "'s current hp: " + current_hp); } } else if (colliderHealth.isEnemy) { // Bullet's hp acts as the "pierce" stat print(gameObject.name + " collided, taking off 1 pierce"); current_hp--; } // Delete the object when hp is 0 or less. if (current_hp <= 0) { print("Delete " + gameObject.name); Destroy(gameObject); } }
void Shoot(Collider2D target) //este método vai ser responsavel por atirar no inimigo { GameObject bulletPrefab = towerData.bala; //variavel que vai receber o prefab da bala Vector3 startPosition = towerData.CurrentLevel.muzzle.position; //variavel da posição de inicio recebendo a posição atual do monstro Vector3 targetPosition = target.transform.position; //variavel da posição do alvo recebendo a posição do alvo startPosition.z = bulletPrefab.transform.position.z; //a posição no eixo z da posição inicial vai ser igual a posição em z da bala targetPosition.z = bulletPrefab.transform.position.z; //a posição no eixo Z do alvo vai receber a posição em z da bala GameObject newBullet = (GameObject)Instantiate(bulletPrefab, startPosition, Quaternion.identity); //instanciando uma nova bala como um GameObject //newBullet.transform.position = startPosition; //a posição da bala vai ser igual a posição inicial do monstro BulletBehavior bulletComp = newBullet.GetComponent <BulletBehavior> (); //Cria-se um objeto do tipo comportamento da bala que vai receber a nova bala pegando o componente BulletBehavior bulletComp.alvo = target.gameObject; //este objeto de comportamento da bala em seu atributo de alvo vai receber o gameObject target e bulletComp.posicaoInicial = startPosition; //em seu atributo de posição inicial vai receber a variavel startPosition bulletComp.posicaoAlvo = targetPosition; //e em seu atributo de posição do alvo vai receber a variavel targetPosition bulletComp.Dano = towerData.CurrentLevel.dano; //por fim executa uma animação que representa o monstro atirando /*Animator animator = * towerData.CurrentLevel.visualizacao.GetComponent <Animator> (); * animator.SetTrigger ("fireShot");*/ //e toca um som AudioSource audioSource = gameObject.GetComponent <AudioSource> (); audioSource.PlayOneShot(audioSource.clip); }
public void DuplicateBullet(Transform otherBulletTransform, float otherBulletDamage) { // make a new position with the x and z of the center of the tower, but the y position of the bullet Vector3 centerOfTower = new Vector3(transform.position.x, otherBulletTransform.position.y, transform.position.z); Vector3 spawnDirection = centerOfTower - otherBulletTransform.position; spawnDirection.Normalize(); Vector3 spawnPosition = centerOfTower + (spawnDirection * towerRadius); // Rotation option 1 // BulletBehavior newBulletBehavior = Instantiate( // bulletPrefab, spawnPosition, otherBulletTransform.rotation ).GetComponent<BulletBehavior>(); //////////////////////////////////////////////////// // Rotation option 2 BulletBehavior newBulletBehavior = Instantiate( bulletPrefab, spawnPosition, Quaternion.identity).GetComponent <BulletBehavior>(); newBulletBehavior.faceTowards(spawnDirection); /////////////////////////////////////////////////// newBulletBehavior.bulletDamage = otherBulletDamage; newBulletBehavior.isClone = true; }
void b_OnRangeExceeded(BulletBehavior b) { bullets.Remove(b); cached.Add(b); b.Reset(); }
void OnTriggerEnter(Collider other) { print("trigger entered"); BulletBehavior bullet = other.GetComponent <BulletBehavior>(); if (bullet != null) { if (bullet.playersBullet) { if (isPlayer) { return; } } if (!bullet.playersBullet) { if (!isPlayer) { return; } } Death(); } }
void Shoot(Collider2D target) { GameObject bulletPrefab = monsterData.CurrentLevel.bullet; // 1 Vector3 startPosition = gameObject.transform.position; Vector3 targetPosition = target.transform.position; startPosition.z = bulletPrefab.transform.position.z; targetPosition.z = bulletPrefab.transform.position.z; // 2 GameObject newBullet = (GameObject)Instantiate(bulletPrefab); newBullet.transform.position = startPosition; BulletBehavior bulletComp = newBullet.GetComponent <BulletBehavior>(); bulletComp.target = target.gameObject; bulletComp.startPosition = startPosition; bulletComp.targetPosition = targetPosition; // 3 AudioSource audioSource = gameObject.GetComponent <AudioSource>(); audioSource.PlayOneShot(audioSource.clip); }
private System.Collections.IEnumerator fireBullet(float accuracy, float damage) { isFiring = true; if (!runAndGun) { enemyMovement.enabled = false; } attackAnimationLength = animationManager.playAttackAnimation(); yield return(new WaitForSeconds(attackAnimationDelay)); fireSound.PlayOneShot(fireSoundClip, Settings.volume); // Instantiate the projectile // Assumes bot is facing the player, so fire in that direction if (bulletPrefab != null) { GameObject bullet = Instantiate( bulletPrefab, projectileSpawnPoint.position, transform.rotation); bullet.SetActive(false); bullet.transform.parent = transform; bullet.transform.LookAt(player.transform); bullet.name = $"{gameObject.name}'s {bulletPrefab.name}"; BulletBehavior bulletScript = bullet.GetComponent <BulletBehavior>(); if (bullet.tag == "Missile") { bulletScript.target = player.transform; } bullet.tag = "Enemy Projectile"; bulletScript.damage = damage * Random.Range(0.8f, 1.2f); // Fire the bullet and apply accuracy Vector3 rotationVector = bullet.transform.rotation.eulerAngles; rotationVector.x += ((Random.value * 2) - 1) * accuracy; rotationVector.y += ((Random.value * 2) - 1) * accuracy; rotationVector.z += ((Random.value * 2) - 1) * accuracy; bullet.transform.rotation = Quaternion.Euler(rotationVector); bullet.SetActive(true); Destroy(bullet, bulletScript.bulletLifetime); } else // Do a melee attack using the projectile spawn point as the epicenter { if (Physics.CheckSphere(projectileSpawnPoint.position, attackRange, playerMask)) { playerHealth.takeDamage(damage); } } if (!runAndGun) { yield return(new WaitForSeconds(attackAnimationLength - attackAnimationDelay)); enemyMovement.enabled = true; } isFiring = false; timeSinceLastAttack = 0; yield return(null); }
void OnCollisionEnter(Collision col) { if (col.gameObject.tag == "Bullet" || col.gameObject.tag == "Missile") { if (col.gameObject == lastBulletCollision) { return; } lastBulletCollision = col.gameObject; // Reduce health BulletBehavior bullet = col.gameObject.GetComponent <BulletBehavior>(); if (bullet == null) // try checking parents { bullet = col.gameObject.GetComponentInParent <BulletBehavior>(); } takeDamage(bullet.damage, 1); if (col.gameObject.tag == "Bullet") { showDamageExplosion(explosions, 0.4f); } else if (col.gameObject.tag == "Missile") { showDamageExplosion(bigExplosions, 0.65f); } } }
void OnTriggerEnter2D(Collider2D col) { if (gameState.isPlaying == true) { //Get the bullet behavior script BulletBehavior bb = col.transform.GetComponent <BulletBehavior>(); //if the collider doesnt have one, it wasnt a bullet and we need to return early if (!bb) { return; } //get bullet power and position float bulletPower = bb.power / 10; float bulletPosition = col.transform.position.y; //if bullet position is greater than trash position if (bulletPosition > transform.position.y) { //set target y position lower than it was targetPosY = targetPosY - bulletPower; } else { //set target y position higher than it was targetPosY = targetPosY + bulletPower; } } }
public void GetBullet(BulletBehavior beh) { health = health - beh.damage; if (health < 0) { logic.DestoyedObject(gameObject); } }
void OnTriggerEnter(Collider collider) { BulletBehavior bulletBehavior = collider.GetComponent <BulletBehavior>(); if (bulletBehavior != null) { currentHP -= bulletBehavior.damage; } }
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; }
public void AddBullet(Vector2 aiming, Vector3 positionModifyer) { GameObject b = Instantiate(bulletPrefab, positionModifyer, transform.rotation); bullets.Add(b); BulletBehavior acessB = b.GetComponent <BulletBehavior>(); acessB.Setup(aiming, controllerNum); }
void Shoot() { GameObject bulletPath = (GameObject)Instantiate(bulletPrefab, bulletSpawnLocation.position, bulletSpawnLocation.rotation); BulletBehavior bullet = bulletPath.GetComponent <BulletBehavior>(); if (bullet != null) { bullet.Seek(target); } }
public void Fire(Vector3 force) { GameObject firedBullet = Instantiate(_bullet, transform.position, transform.rotation); BulletBehavior bulletScript = firedBullet.GetComponent <BulletBehavior>(); if (bulletScript) { bulletScript.Rigidbody.AddForce(force, ForceMode.Impulse); } }
void Shoot(Vector2 pos) { GameObject bullet = Instantiate(Prefabs.Instance.bullet, owner.transform.position, Quaternion.identity, null); BulletBehavior bulletBehavior = bullet.GetComponent <BulletBehavior>(); bulletBehavior.bulletType = bulletType.Direction; bulletBehavior.travelSpeed = travelSpeed; bulletBehavior.damage = damage; bulletBehavior.dir = pos; bulletBehavior.owner = owner; }
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); }
public void FireBullet() { // Create a new bullet object, set its color based off of the cannon, and set its initial speed BulletBehavior newBullet = Instantiate(bulletObject, bulletSpawnPosition.position, transform.rotation).GetComponent <BulletBehavior>(); newBullet.SetBulletColor(cannonColorSR.color); newBullet.FireBullet(bulletFireForce); // Add variation in the shooting noise to avoid repetition in sound shootNoise.pitch = Random.Range(0.8f, 1.2f); shootNoise.Play(); }
void OnCollisionEnter(Collision col) { if (col.gameObject.tag == "Enemy Projectile") { BulletBehavior bullet = col.gameObject.GetComponent <BulletBehavior>(); if (bullet.hitSomething) { return; } bullet.hitSomething = true; takeDamage(bullet.damage); } }
public void Fire(Vector3 force) { //Makes a new bullet GameObject firedBullet = Instantiate(_bullet, transform.position, transform.rotation); BulletBehavior bulletScript = firedBullet.GetComponent <BulletBehavior>(); //If the bullet is not false, it adds a force to it if (bulletScript) { bulletScript.Rigidbody.AddForce(force, ForceMode.Impulse); } }
/// <summary> /// The function to be used to spawn and add the behavior and force to the bullet to fire /// </summary> /// <param name="Force"></param> public void Firing(Vector3 Force) { // The Gameobject is set to be the bullet that is spawned GameObject BulletShot = Instantiate(_bullet, transform.position, transform.rotation); // Gets and adds the bullet behavior to the shot bullet BulletBehavior bulletBehavior = BulletShot.GetComponent <BulletBehavior>(); // If the bullet behavior is not null add a force to its body equal to the false if (bulletBehavior) { bulletBehavior.Rigidbody.AddForce(Force, ForceMode.Impulse); } }
private void Shoot(Collider target) { Vector3 startPosition = gameObject.transform.position; Vector3 targetPosition = target.transform.position; GameObject newBullet = (GameObject)Instantiate(BulletPrefab); newBullet.transform.position = startPosition; BulletBehavior bullet = newBullet.GetComponent <BulletBehavior>(); bullet.Target = target.gameObject; bullet.StartPosition = startPosition; bullet.TargetPosition = targetPosition; }
void Die() { if (Bullet != null && !Modle) { for (int j = 0; j < NumberOfBullet; j++) { Vector3 v = new Vector3(Random.Range(0f, 360f), Random.Range(0f, 360f), Random.Range(0f, 360f)); GameObject clone1 = Instantiate(Bullet, transform.position, Quaternion.Euler(v)) as GameObject; BulletBehavior b = clone1.GetComponent <BulletBehavior>(); b.Modle = false; b.Speed += Random.Range(-2.5f, 2.5f); } } }
void ShootParticle(Vector3 targetPosition) { Vector3 startPosition = gameObject.transform.position; startPosition.z = bulletPrefab.transform.position.z; targetPosition.z = bulletPrefab.transform.position.z; // 2 GameObject newBullet = (GameObject)Instantiate(bulletPrefab); newBullet.transform.position = startPosition; BulletBehavior bulletComp = newBullet.GetComponent <BulletBehavior>(); bulletComp.startPosition = startPosition; bulletComp.targetPosition = targetPosition; }