60 days to become a game developer. Day 24

Ryan Brechler
3 min readDec 4, 2020

ENEMY PLAYER 👾👾👾👾👾👾👾👾

Today I worked on understanding the logic for creating an enemy player. From the start you want to create a new 3D object and just create another cube. From there you want to shrink it a little bit by size so you can tell the difference and then you want to change the color. I went with red so you know what is what from the player prefab. From there you want to remember to create a new script and to drop into the prefab which is your enemy. That is the safest way to make sure the script connects with the prefab. Once I went into the code I needed to create the logic and how to implement the movement of the player. I then created tags. This makes so much more sense now when implementing the code. This taught me one thing.. Spell it exactly how you spell it in Unity. The code below shows how to implement a random spawn method and shows how the enemy spawns.

Code for the movement of the enemy.
Video of enemy spawning at random locations.

We then went into creating the code for the player and enemy. The logic here is that we wanted to loose a life when the player collides with the enemy. First we need to create the lives for the player.

[SerializeField]

private int _lives = 3;

We then need to create the logic for the lives so when you get hit it takes a life away.

public void Damage()

{

_lives — ;

if (_lives < 1)

{

Destroy(this.gameObject);

}

}

We need to then move over to the enemy script and create the collision with the player.

private void OnTriggerEnter(Collider other)

{

if (other.tag == “Player”)

{

Player player = other.transform.GetComponent<Player>();

if (player != null)

{

player.Damage();

}

Destroy(this.gameObject);

}

if (other.tag == “Laser”)

{

Destroy(other.gameObject);

Destroy(this.gameObject);

}

}

Player loosing a life every time it hits the enemy

We also created the code for colliding with the laser as well so when it colides it destroys the enemy object.

Code for when the laser hits the enemy.
Player hits the enemy with laser

--

--

Ryan Brechler

I live my life by a quote I read off a wall in Iraq… “I refuse to tip toe through life only to arrive safely to death” Currently on the path to a new me!