using UnityEngine;
using System.Collections;
public class Enemy01 : MonoBehaviour {
public float moveSpeed = 2f; // The speed the enemy moves at.
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// Set the enemy's velocity to moveSpeed in the x direction.
GetComponent<Rigidbody2D>().velocity = new Vector2(transform.localScale.x * moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
}
using UnityEngine;
using System.Collections;
public class Enemy02 : MonoBehaviour {
public float moveSpeed = 2f; // The speed the enemy moves at.
private Transform frontCheck; // Reference to the position of the gameobject used for checking if something is in front.
// Use this for initialization
void Start () {
frontCheck = transform.Find("frontCheck").transform;
}
// Update is called once per frame
void Update () {
// Create an array of all the colliders in front of the enemy.
Collider2D[] frontHits = Physics2D.OverlapPointAll(frontCheck.position, 1);
// Check each of the colliders.
foreach(Collider2D c in frontHits)
{
// If any of the colliders is an Obstacle...
if(c.tag == "Obstacle")
{
// ... Flip the enemy and stop checking the other colliders.
Flip ();
break;
}
}
// Set the enemy's velocity to moveSpeed in the x direction.
GetComponent<Rigidbody2D>().velocity = new Vector2(transform.localScale.x * moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
public void Flip()
{
// Multiply the x component of localScale by -1.
Vector3 enemyScale = transform.localScale;
enemyScale.x *= -1;
transform.localScale = enemyScale;
}
}
using UnityEngine;
using System.Collections;
public class SetParticleSortingLayer : MonoBehaviour
{
public string sortingLayerName; // The name of the sorting layer the particles should be set to.
void Start ()
{
// Set the sorting layer of the particle system.
particleSystem.renderer.sortingLayerName = sortingLayerName;
}
}
using UnityEngine;
using System.Collections;
public class Rocket01 : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (new Vector3(0.1f, 0,0));
}
void OnTriggerEnter2D (Collider2D col)
{
// If it hits an enemy...
if(col.tag == "Enemy")
{
print("Enemy ...");
}
}
}
using UnityEngine;
using System.Collections;
public class Destroyer01 : MonoBehaviour {
void DestroyGameObject ()
{
// Destroy this gameobject, this can be called from an Animation Event.
Destroy (gameObject);
}
}
using UnityEngine;
using System.Collections;
public class Rocket02 : MonoBehaviour {
public GameObject explosion; // Prefab of explosion effect.
// Use this for initialization
void Start () {
}
void OnExplode()
{
// Create a quaternion with a random rotation in the z-axis.
Quaternion randomRotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
// Instantiate the explosion where the rocket is with the random rotation.
Instantiate(explosion, transform.position, randomRotation);
}
// Update is called once per frame
void Update () {
transform.Translate (new Vector3(0.1f, 0,0));
}
void OnTriggerEnter2D (Collider2D col)
{
// If it hits an enemy...
if(col.tag == "Enemy")
{
print("Enemy ...");
// Call the explosion instantiation.
OnExplode();
// Destroy the rocket.
Destroy (gameObject);
}
else if(col.gameObject.tag != "Player")
{
// Instantiate the explosion and destroy the rocket.
OnExplode();
Destroy (gameObject);
}
}
}