private void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Bullet" || other.tag == "EnemyBullet") { Destroy(other.gameObject); bulletCounter.DecreaseBulletAmount(); } }
private void OnTriggerEnter2D(Collider2D other) // When touching a trigger object { if (other.CompareTag("Bullet")) // Check to see if the object is a bullet { if (!other.GetComponent <MoveBullet>().canGoThroughEnemies) // if bullet cannot go through enemies { Destroy(other.gameObject); // Destroy Bullet bulletCounter.DecreaseBulletAmount(); // Decrease bullet counter } else { other.GetComponent <MoveBullet>().GoThroughEnemy(); } TakeDamage(other.GetComponent <MoveBullet>().damage); } else if (other.CompareTag("PlayerMelee")) { // Take damage and add force TakeDamage(playerController.attackDamage); if (playerController.isFacingLeft) { playerDirection = -1; } else { playerDirection = 1; } if (affectedByForce) { forceScript.PushEnemy(playerController.attackForce.x, playerController.attackForce.y, playerDirection); } if (!playerController.isGrounded) { playerController.AirRebound(); // Call Air Rebound Function } } if (health <= 0 && !invincible) // if health is less than or equal to 0 { for (int i = 0; i < coins.Length; i++) { Instantiate(coins[i], transform.position, transform.rotation); } Destroy(gameObject); // Kill enemy } }
private void OnTriggerEnter2D(Collider2D other) // When touching a trigger object { if (other.CompareTag("Bullet")) // Check to see if the object is a bullet { if (!other.GetComponent <MoveBullet>().canGoThroughEnemies) // if bullet cannot go through enemies { Destroy(other.gameObject); // Destroy Bullet bulletCounter.DecreaseBulletAmount(); // Decrease bullet counter } TakeDamage(other.GetComponent <MoveBullet>().damage); spriteAnimator.ChangeAnimationState(TEST_OBJECT_HURT); // Play hurt animation } else if (other.CompareTag("PlayerMelee")) { // Take damage and add force TakeDamage(playerController.attackDamage); if (playerController.isFacingLeft) { playerDirection = -1; } else { playerDirection = 1; } Vector2 force = new Vector2(playerController.attackForce.x * playerDirection, playerController.attackForce.y); rb2d.AddForce(force + new Vector2(0.0f, -rb2d.velocity.y), ForceMode2D.Impulse); if (!playerController.isGrounded) { playerController.AirRebound(); // Call Air Rebound Function } spriteAnimator.ChangeAnimationState(TEST_OBJECT_HURT); // Play hurt animation } if (health <= 0) // if health is less than or equal to 0 { for (int i = 0; i < coins.Length; i++) { Instantiate(coins[i], transform.position, transform.rotation); } Destroy(gameObject); // Kill enemy } Invoke("ReturnToIdleAnimation", 0.1f); // reset to idle animation after hit }
// Update is called once per frame void FixedUpdate() { rb2d.MovePosition(rb2d.position + Vector2.right * bulletSpeed * direction * Time.deltaTime); // New position is the current one, plus a direction, times by speed and direction // Countdown bullet's despawn time and then delete it when it reaches 0 if it is offscreen if (bulletOffscreen) { if (bulletDespawnTime > 0) { bulletDespawnTime -= bulletDespawnRate; } else { Destroy(gameObject); bulletCounter.DecreaseBulletAmount(); } } }