Tuesday 17 November 2015

Destroying an Object in Unity

Whether or not you want an enemy to vanish when the player kills them, or have a summoned weapon projectile disappear after they've spawned and done the job, destroying objects in one way or another is something that is done very frequently in games.

As provided by (IgorAherne, 2015) from Unity Answers (an official Unity forum that may not be a 100% credible source): the character you want to die and object you want to kill the player will both need colliders attached to them, ensuring the 'is trigger' option is not enabled in the settings for the player, while it is checked for the enemy. And as sourced by (HarshadK, 2015), another Unity answers page, the "
  1. Destroy(gameObject);
" command is very useful as if the player wants to kill the enemy, that line of code will make them disappear. And: "
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Collision : MonoBehaviour {
  4. void OnCollisionEnter2D(Collision2D coll) {
  5. if (coll.gameObject.tag == "Lava")
  6. Destroy(gameObject);
  7. }
  8. }
" Is example code from (HarshadK, 2015), for killing and despawning a player when it collides with an object called lava. This is how a player can register a collision and cause them to die, thus making them start over. This is very important for a game to have, as without player death a video game would just become far too easy and lack any sort of challenge or competition!

No comments:

Post a Comment