The first enemy that I wanted to create was an enemy that chases the player (like in the original game of gauntlet) so I quickly came up with a image to be used for the enemy;
The first thing I did to create my enemy was to add an empty game object to my scene. I then added a plane to the scene applied my enemy image and dragged it over the empty game object to make it it's child.
I then started on the coding for my enemy, I created a new javascript file and added the following code;
private var player:GameObject;
function Start() {
player=GameObject.FindGameObjectWithTag("player");
this.transform.position.z=player.transform.position.z;
}
function Update () {
transform.LookAt(player.transform);
}
This code basically says the enemy should look for the game object with the tag 'player' which is the tag I gave to the player. Then it says when it has found the game object with that tag to look at it.
Upon testing the game the enemies look at the player but I want them to chase the player so I had to add some extra code.
If I want the agent to move towards the player then I'll have to find the Vector between the player and the agent. If I then set that to a magnitude of 1 it will allow me to set a constant translation value for the enemies. I then added the following 2 functions to my code;
function FixedUpdate() {
var playerVector:Vector3=findVectorToPlayer();
this.transform.Translate(playerVector.normalized*15*Time.deltaTime,Space.World);
}
function findVectorToPlayer():Vector3{
return Vector3(player.transform.position.x-this.transform.position.x,
player.transform.position.y-this.transform.position.y,
0);
}
I then created a new script for the enemycollision
I wanted to go a bit different to gauntlet on the enemy front and instead of having enemies just chase you around I just wanted them to fire bullets at you (or in this case sim cards).
The first thing I did was create a simple sprite for my enemy;
and a simple bullet for them to fire;
The first thing I did to create my enemy was to add an empty game object to my scene. I then added a plane to the scene applied my enemy image and dragged it over the empty game object to make it it's child.
I then started on the coding for my enemy, I created a new javascript file and added the following code;
private var player:GameObject;
function Start() {
player=GameObject.FindGameObjectWithTag("player");
this.transform.position.z=player.transform.position.z;
}
function Update () {
transform.LookAt(player.transform);
}
This code basically says the enemy should look for the game object with the tag 'player' which is the tag I gave to the player. Then it says when it has found the game object with that tag to look at it.
Upon testing the game the enemies look at the player but I want them to chase the player so I had to add some extra code.
If I want the agent to move towards the player then I'll have to find the Vector between the player and the agent. If I then set that to a magnitude of 1 it will allow me to set a constant translation value for the enemies. I then added the following 2 functions to my code;
function FixedUpdate() {
var playerVector:Vector3=findVectorToPlayer();
this.transform.Translate(playerVector.normalized*15*Time.deltaTime,Space.World);
}
function findVectorToPlayer():Vector3{
return Vector3(player.transform.position.x-this.transform.position.x,
player.transform.position.y-this.transform.position.y,
0);
}
I then created a new script for the enemycollision
I wanted to go a bit different to gauntlet on the enemy front and instead of having enemies just chase you around I just wanted them to fire bullets at you (or in this case sim cards).
The first thing I did was create a simple sprite for my enemy;
and a simple bullet for them to fire;
I then created an enemy game object and applied the enemy image from above. I then made this into a prefab so that I was able to put more than one in my game.
The first bit of script that I added to the enemy was as follows;
To create the bullet I first created a cube and turned into a prefab as there will be more than one at any given time during the game play.



No comments:
Post a Comment