Creating Enemy and Structures
Submitted by razormist on Thursday, March 15, 2018 - 22:05.
Creating Enemy
We will now create the object that will be target for the cannon. To do that first locates the sprite inside the Sprites directory.- Drag the sprite GameObject to the Scene View.
- Next set the component of the Enemy GameObject.
Note: To the other enemies sprites make you sure the transform components is same above the image.
- Next select the Enemy then add an Animator Controller in the Inspector.
- Go to Animations and create a new directory that correspond to your enemy sprite. In the Sprites directory they are type of enemies the blue, green and yellow. So create a directory that base on the enemy properties as Blue, Green and Yellow.
- Then click Create New Clip and name it as correspond to your enemy name, in my example I used name it as Blue Idle and save inside Animations corresponding directory to avoid misplaced.
- Then click again the Create New Clip to create another animations and name it as Blue Hurt and save it in the Animations corresponding directory. After that set each Animation Clip components shown below.
Blue Idle
Blue Hurt
- After that we will create the script for the enemies. Go to the Scripts folder and create a new folder inside of it called Enemy.
- Create a C# script called EnemyController.
- Write these block of code inside the EnemyController Class:
- public int maxHitPoints;
- public float damageCounter;
- public GameObject deathEffect;
- public AudioClip hurt;
- private Animator animator;
- private int hitPoints;
- private Vector3 bounce;
- private int maxScore;
- void Awake(){
- animator = GetComponent<Animator> ();
- }
- // Use this for initialization
- void Start () {
- InitializeVariables ();
- }
- // Update is called once per frame
- void Update () {
- }
- void InitializeVariables(){
- hitPoints = maxHitPoints;
- maxScore = 1000;
- }
- void UpdateAnimationState(){
- if(hitPoints <= 5){
- animator.SetTrigger ("isDamage");
- }
- }
- void OnCollisionEnter2D(Collision2D collision){
- if(collision.relativeVelocity.magnitude > damageCounter){
- hitPoints -= Mathf.RoundToInt(collision.relativeVelocity.magnitude);
- UpdateScoreStatus (Mathf.RoundToInt (collision.relativeVelocity.magnitude));
- if(GameController.instance != null && MusicController.instance != null){
- if(GameController.instance.isMusicOn){
- if (gameObject != null) {
- AudioSource.PlayClipAtPoint (hurt, transform.position);
- }
- }
- }
- }
- UpdateAnimationState ();
- if(hitPoints <= 0){
- Death ();
- if(collision.gameObject.CompareTag("Player Bullet")){
- bounce = collision.transform.GetComponent<Rigidbody2D> ().velocity;
- bounce.y = 0f;
- collision.transform.GetComponent<Rigidbody2D> ().velocity = bounce;
- }
- }
- }
- void Death(){
- Destroy (gameObject);
- GameObject newDeathEffect = Instantiate (deathEffect, transform.position, Quaternion.identity) as GameObject;
- Destroy (newDeathEffect, 3f);
- if(GameController.instance != null){
- GameController.instance.score += maxScore;
- }
- DisplayScore ();
- }
- void UpdateScoreStatus(int hitScore){
- if(GameController.instance != null){
- GameController.instance.score += hitScore;
- }
- }
- void DisplayScore(){
- GameObject scoreText = Instantiate (Resources.Load ("Score Text Canvas"), new Vector3(transform.position.x, transform.position.y + 1f), Quaternion.identity) as GameObject;
- scoreText.transform.GetChild (0).transform.GetComponent<Text> ().text = maxScore.ToString ();
- Destroy (scoreText, 2f);
- }
- After creating the script, drag the script to the Blue inspector as a component.
- We will create now the death effect for the enemy go to GameObject and Select Particle System.
- Rename the Particle System as EnemyDeathEffect and set the components of the Particle System as shown below.
- After creating the effect, drag the effects inside the FX directory to create a prefab and delete it because we will just spawn the gameobject.
- Lastly set the EnemyController script in the inspector as shown below.
Creating Structure
We will now create the object that will be deflecting any cannon bullets that try to destroy enemy. To do that first locates the sprite inside the Sprites directory.- Drag the sprite GameObject to the Scene View Then rename the GameObject as Wood.
- After that we will create the script for the structure. Go to the Scripts folder and create a new folder inside of it called Object.
- Create a C# script called Structure.
- Write these block of code inside the Structure Class:
- public int maxHitPoints;
- public Sprite[] sprite;
- public float damageCounter;
- public GameObject disassemble;
- public AudioClip stoneSound, woodSound, glassSound;
- public enum StructureType{
- Wood,
- Stone,
- Glass
- }
- public StructureType structure;
- public int hitpoints;
- private SpriteRenderer spriteRenderer;
- private Vector3 bounce;
- private int maxScore;
- private int counter;
- void Awake(){
- spriteRenderer = GetComponent<SpriteRenderer> ();
- }
- // Use this for initialization
- void Start () {
- InitializeVariables ();
- }
- // Update is called once per frame
- void Update () {
- }
- void InitializeVariables(){
- hitpoints = maxHitPoints;
- maxScore = 500;
- counter = 2;
- }
- void OnCollisionEnter2D(Collision2D collision){
- if(collision.relativeVelocity.magnitude > damageCounter){
- hitpoints -= Mathf.RoundToInt (collision.relativeVelocity.magnitude);
- UpdateScoreStatus (Mathf.RoundToInt (collision.relativeVelocity.magnitude));
- }
- if (hitpoints <= 50) {
- spriteRenderer.sprite = sprite [0];
- if(counter == 2){
- AudioManager ();
- counter--;
- }
- }
- if(hitpoints <= 30){
- spriteRenderer.sprite = sprite [1];
- if(counter == 1){
- AudioManager ();
- counter--;
- }
- }
- if(hitpoints <= 0){
- Destroyed ();
- if(collision.gameObject.CompareTag("Player Bullet")){
- bounce = collision.transform.GetComponent<Rigidbody2D> ().velocity;
- bounce.y = 0f;
- collision.transform.GetComponent<Rigidbody2D> ().velocity = bounce;
- }
- }
- }
- void Destroyed(){
- Destroy (gameObject);
- GameObject newDisassemble = Instantiate (disassemble, transform.position, Quaternion.identity) as GameObject;
- Destroy (newDisassemble, 3f);
- if(GameController.instance != null){
- GameController.instance.score += maxScore;
- }
- GameObject scoreText = Instantiate (Resources.Load("Score Text Canvas"), new Vector3 (transform.position.x, transform.position.y + 1f, transform.position.z), Quaternion.identity) as GameObject;
- scoreText.transform.GetChild (0).transform.GetComponent<Text> ().text = maxScore.ToString();
- Destroy (scoreText, 2f);
- }
- void UpdateScoreStatus(int hitScore){
- if(GameController.instance != null){
- GameController.instance.score += hitScore;
- }
- }
- void AudioManager(){
- switch (structure) {
- case StructureType.Wood:
- if(GameController.instance != null && MusicController.instance != null){
- if(GameController.instance.isMusicOn){
- if (gameObject != null) {
- AudioSource.PlayClipAtPoint (woodSound, transform.position);
- }
- }
- }
- break;
- case StructureType.Stone:
- if(GameController.instance != null && MusicController.instance != null){
- if(GameController.instance.isMusicOn){
- if (gameObject != null) {
- AudioSource.PlayClipAtPoint (stoneSound, transform.position);
- }
- }
- }
- break;
- case StructureType.Glass:
- if(GameController.instance != null && MusicController.instance != null){
- if(GameController.instance.isMusicOn){
- if (gameObject != null) {
- AudioSource.PlayClipAtPoint (glassSound, transform.position);
- }
- }
- }
- break;
- }
- }
- After creating the script, drag the script to the Wood inspector as a component.
- We will create now the effect for the wood, first go to GameObject and Select Particle System.
- Rename the Particle System as Wood Disassemble Effect and set the components of the Particle System as shown below.
- After creating the effect, drag the effects inside the FX directory to create a prefab and delete it.
- Then set the Structure script in the inspector as shown below.
- Lastly duplicate the Wood GameObject into two then set each components as show below.
Wood Wall Medium
Wood Wall Long
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Add new comment
- 157 views