In yesterday's article - Unity3D: Space Invaders (Part 4 - Collisions) - we learned how to handle collisions in Unity. At the end of the article, we listed several items that are still missing from Ranch Invaders.
Today's article deals with some of those issues, mainly relating to Player and Alien behaviour.
Player Movement
In Space Invaders, the Player's ship moves only left and right. If you've read "Unity3D: Moving an Object with Keyboard Input", you'll know that this is really easy to do. Open the Player script with MonoDevelop. You should currently have the code for shooting:void Update ()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Instantiate(bullet, this.transform.position, Quaternion.identity);
}
}
Change it as follows to allow the Player to move using the left and right arrows:
void Update ()
{
Vector3 pos = this.transform.position;
if (Input.GetKeyDown(KeyCode.Space))
Instantiate(bullet, this.transform.position, Quaternion.identity);
if (Input.GetKeyDown(KeyCode.LeftArrow))
this.transform.position = new Vector3(pos.x - 0.1f, pos.y, pos.z);
if (Input.GetKeyDown(KeyCode.RightArrow))
this.transform.position = new Vector3(pos.x + 0.1f, pos.y, pos.z);
}
Great, you can now move the Player's ship left and right. There's a problem, however. If you hold down an arrow key, the Player's ship does not continue moving. We can fix this by using Input.GetKey() instead. Also, we need to set constraints to prevent the Player's ship from leaving the screen:
void Update ()
{
Vector3 pos = this.transform.position;
if (Input.GetKey(KeyCode.Space))
Instantiate(bullet, this.transform.position, Quaternion.identity);
if (Input.GetKey(KeyCode.LeftArrow) && pos.x > -2.5f)
this.transform.position = new Vector3(pos.x - 0.1f, pos.y, pos.z);
if (Input.GetKey(KeyCode.RightArrow) && pos.x < 2.5f)
this.transform.position = new Vector3(pos.x + 0.1f, pos.y, pos.z);
}
Cooldown
As it is, the Player can fire as many bullets as it likes, with no gap between one and another. In order to implement a cooldown, we use an approach similar to this. When a bullet is fired, the time is recorded and no bullet may be fired before the cooldown period has passed. To do this, declare the following variables in the Player class:public float cooldown = 1.0f; // minimum time between bullets
private float nextFire = 1.0f; // next time when a bullet can be fired
Then, adjust the firing code as follows:
if (Input.GetKey(KeyCode.Space) && Time.time >= nextFire)
{
nextFire += cooldown;
Instantiate(bullet, this.transform.position, Quaternion.identity);
}
Then, try it out, and adjust cooldown as you like:
Then, rejoice.
Alien Movement
In Space Invaders, aliens move right, down, left, down, etc. Making this work properly takes a bit of work, so I'll just give you the code:public class Alien : MonoBehaviour
{
public float horDistance = 1.0f;
public float verDistance = 0.5f;
public float speed = 0.4f;
private float startTime;
private Vector3 startingPosition;
private Vector3 target;
// Use this for initialization
void Start ()
{
this.startTime = Time.time;
this.startingPosition = this.transform.position;
this.target = this.startingPosition + new Vector3(this.horDistance, 0, 0);
}
// Update is called once per frame
void Update ()
{
Vector3 currentPosition = this.transform.position;
Vector3 newPosition = Vector3.MoveTowards(currentPosition, this.target, speed * Time.deltaTime);
this.transform.position = newPosition;
//print (this.target);
if (newPosition == target)
{
if (newPosition.x == startingPosition.x)
{
if (((newPosition.y - startingPosition.y) / verDistance) % 2 == 0)
this.target = newPosition + new Vector3(this.horDistance, 0, 0);
else
this.target = newPosition - new Vector3(0, this.verDistance, 0);
}
else
{
if (((newPosition.y - startingPosition.y) / verDistance) % 2 != 0)
this.target = newPosition - new Vector3(this.horDistance, 0, 0);
else
this.target = newPosition - new Vector3(0, this.verDistance, 0);
}
}
}
}
Don't fret about what the code is doing... just use it. Tweak the public variables as you like.
More Aliens
Since we need a lot of aliens, it's a good idea to make the Alien object a prefab. To do this, drag it from the Hierarchy panel to the Assets panel. If you did it right, its listing in the Hierarchy panel should turn blue. This way, if you make any changes, you can apply them across all Aliens.To make multiple Aliens, you can either press Ctrl+D (duplicate) to create additional objects, or else instantiate all the aliens in code using for loops. In my case, I just duplicated a handful of Aliens, just for testing:
Don't get too fussy about the Alien placement and all that. The reason we're still working with cubes and spheres is that you should first concentrate on making sure that your game mechanics work correctly. Only once that is done should you spend some time (a lot of time, actually) making it look good.
Aliens Shooting
Allowing Aliens to shoot the Player is no different from allowing the Player to shoot the Aliens. However, you now need a different bullet that moves downwards and that can destroy the player.Once you attach a rigidbody to the Player (Component -> Physics -> Rigidbody, and remember to turn off "Use Gravity"), you will find some unexpected behaviour: at times, when the Player shoots a bullet, the Player itself is destroyed! That's because we don't yet have any code in the Bullet script that distinguishes between objects it can destroy. So far it worked correctly only because the Player didn't have a rigidbody, in which case the collision doesn't happen.
An easy way to solve this is to tag the object. Select the Player. At the top of the Inspector is a section where you can select a tag to assign to the object. For the Player, you can choose the predefined Player tag:
For the Aliens, you will need to add a new tag. From the drop-down list containing tags (shown above), select "Add Tag...".
In the resulting interface (shown above), expand the "Tags" node. Next to "Element 0", type "Alien". This creates the "Alien" tag, but does not assign it to the object.
To tag the Alien as such, select an Alien object, and select the newly available "Alien" tag from the drop-down list. Remember to click "Apply" to make this work for all Alien objects (since it's a prefab).
The Bullet script's collision code now becomes:
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Alien")
{
GameObject.Destroy(collision.gameObject);
GameObject.Destroy(this.gameObject);
}
}
The Player's bullets should now destroy only Aliens.
For the aliens to shoot at the player, you will first need to create another bullet prefab (e.g. "AlienBullet") with the same script as the Bullet, except for two key differences. First, the bullet needs to go downwards instead of upwards. So:
void Start ()
{
this.target = this.transform.position - new Vector3(0, 20, 0);
}
Secondly, it needs to collide with the Player instead of the Alien:
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
GameObject.Destroy(collision.gameObject);
GameObject.Destroy(this.gameObject);
}
}
Finally, the code for shooting (in the Alien script) will be a little different from that of the Player. Instead of shooting when the user presses a key, the Alien will shoot with a random delay. First, declare the following variables in the Alien class:
public GameObject bullet;
public float minShootDelay = 1.0f;
public float maxShootDelay = 7.0f;
private float nextShootTime = 0.0f;
In the Unity editor, set the AlienBullet prefab in the Alien script's bullet slot.
In the Start() method, add code to initialise nextShootTime:
this.nextShootTime = Random.Range(minShootDelay, maxShootDelay);
Unity's Random.Range() method is similar to .NET's Random.Next() method, but not quite the same.
Change the code that handles when bullets leave the game area, since these bullets are going down instead of up:
if (this.transform.position.y < -3.0f)
GameObject.Destroy(this.gameObject);
Finally, in the Update() method, we add code for actual shooting:
if (Time.time > nextShootTime)
{
Instantiate(bullet, this.transform.position, Quaternion.identity);
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
}
If you try it now, things will get a little bit messy as the Aliens' bullets will collide with themselves:
Explaining how to solve this requires an article in itself. Notwithstanding this, in this (long) article we have taken care of several behaviour issues with both the Player and the Aliens, and brought the game much closer to a working Space Invaders clone.
This is the last article in the Ranch Invaders series. I hope you'll return to the Ranch to read about other interesting topics! :)
WHAT!!! nooooooo!!!! i came this far! i learned so much! its quite a cliffhanger you left me on :P
ReplyDeletewell i dont know if its the right solution, but i put the alien and the alien bullet on the same layer thingie (had to make a new layer, called it enemy, and it was in the 8th slot)... then used:
ReplyDeletePhysics.IgnoreLayerCollision(8, 8, true);
in the alien bullet script, and as if the magic the collisions were ignored between the alien bullet and the alien box! :)
WHAT!!! nooooooo!!!! i came this far! i learned so much! its quite a cliffhanger you left me on :P
ReplyDelete