Homework 3

Juan Abaroa
Oct 17 · 4 min read

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyController : MonoBehaviour {

public Waypoint[] path;
private float treshold = 1;
private int current;
private Animator anim;
private Rigidbody rigidBody;
private float speed = 3;
// Use this for initialization
void Start () {
current = 0;
anim = GetComponent<Animator> ();
rigidBody = GetComponent<Rigidbody> ();
}

// Update is called once per frame
void Update () {
transform.LookAt(path[current].transform);
transform.Translate(transform.forward * Time.deltaTime * speed, Space.World);

float distance = Vector3.Distance(transform.position, path[current].transform.position);

if(distance < treshold) {
current++;
current %= path.Length;
}
}

void OnCollisionEnter(Collision c) {
switch(c.transform.gameObject.tag) {
case “Projectile”:
speed = 0;
rigidBody.velocity = new Vector3();
Destroy (c.transform.gameObject);
Debug.Log (“Collision with Projectile at” + Time.time);
anim.SetTrigger (“Hit”);
StartCoroutine (destroyGameObject());
break;
}
}

IEnumerator destroyGameObject() {
yield return new WaitForSeconds(1.5f);
Debug.Log (“Destroyed enemy at “ + Time.time);
Destroy (gameObject);
}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyProjectileController : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

void OnBecameInvisible() {
Destroy (transform.gameObject);
}
}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyProjectileSpawner : MonoBehaviour {

public GameObject enemyProjectile;
private float xValue;
// Use this for initialization
void Start () {
StartCoroutine (fireEnemyProjectile ());
}

// Update is called once per frame
void Update () {

}

// Gyzmos
void OnDrawGizmos() {
// Draw a sphere around the user.
Gizmos.color = Color.green;
Gizmos.DrawSphere(transform.position, 0.5f);
Gizmos.color = Color.yellow;
}

void OnDrawGizmosSelected() {
Gizmos.color = Color.yellow;
Gizmos.DrawSphere(transform.position, 0.5f);
}

IEnumerator fireEnemyProjectile() {
while (true) {
yield return new WaitForSeconds (0.5f);
xValue = Mathf.Clamp (Random.value * 10, 0, 6.4f);
GameObject clone = Instantiate (enemyProjectile, new Vector3 (transform.position.x + xValue, 0.25f, transform.position.z), transform.rotation) as GameObject;
clone.GetComponent<Rigidbody> ().AddForce ( -1 * transform.forward * 30, ForceMode.Impulse);
}
}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

public GameObject projectile;
private float speed = 3.0f;
private Animator anim;
private Rigidbody rigidBody;
public int life = 10;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
rigidBody = GetComponent<Rigidbody> ();
life = 10;
anim.SetInteger (“Life”, life);
}

// Update is called once per frame
void Update () {
rigidBody.velocity = new Vector3 ();
Ray mousePositionRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (mousePositionRay, out hit)) {
if (hit.transform.name == “Ground”) {
transform.LookAt (hit.point);
}
}

float h = Input.GetAxisRaw (“Horizontal”);
float v = Input.GetAxisRaw (“Vertical”);
float max = Mathf.Max (Mathf.Abs(h), Mathf.Abs(v));

anim.SetFloat (“Speed”, max);

transform.Translate (h * speed * Time.deltaTime, 0, v * speed * Time.deltaTime, Space.World);

if (Input.GetMouseButtonUp (0)) {
Debug.Log (“Click 0 at time: “ + Time.time);
anim.SetTrigger (“Attack”);
GameObject clone = Instantiate (projectile, new Vector3(transform.position.x, 0.25f, transform.position.z), transform.rotation) as GameObject;
}
}

void OnCollisionEnter(Collision c) {
switch(c.transform.gameObject.tag) {
case “EnemyProjectile”:
Destroy (c.transform.gameObject);
Debug.Log (“Collision with EnemyProjectile at” + Time.time);
anim.SetTrigger (“Hit”);
life — ;
if (life < 0) {
anim.SetTrigger (“Dead”);
StartCoroutine (destroyGameObject());
}
break;
case “Enemy”:
Debug.Log (“Collision with Enemy at” + Time.time);
anim.SetTrigger (“Hit”);
life — ;
if (life < 0) {
anim.SetTrigger (“Dead”);
StartCoroutine (destroyGameObject());
}
break;

}
}

IEnumerator destroyGameObject() {
yield return new WaitForSeconds(1.0f);
Debug.Log (“Destroyed enemy at “ + Time.time);
Destroy (gameObject);
}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Waypoint : MonoBehaviour {

public Waypoint[] neighbors;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

// Gyzmos
void OnDrawGizmos() {
// Draw a sphere around the user.
Gizmos.color = Color.blue;
Gizmos.DrawSphere(transform.position, 0.5f);

Gizmos.color = Color.yellow;
for(int i = 0; i < neighbors.Length; i++) {
Gizmos.DrawLine(transform.position, neighbors[i].transform.position);
}

}

void OnDrawGizmosSelected() {
Gizmos.color = Color.red;
Gizmos.DrawSphere(transform.position, 0.5f);
}
}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ProjectileController : MonoBehaviour {

Rigidbody rigidBody;
// Use this for initialization
void Start () {
rigidBody = GetComponent<Rigidbody> ();
rigidBody.AddForce (transform.forward * 30, ForceMode.Impulse);
}

// Update is called once per frame
void Update () {
}

void OnBecameInvisible() {
Destroy (transform.gameObject);
}

// void OnCollisionEnter(Collision c) {
// switch(c.transform.gameObject.tag) {
// case “Enemy”:
// Destroy(c.transform.gameObject);
// Debug.Log(“Collision with Brick”);
// break;
// }
// Destroy(gameObject);
// }
}

Juan Abaroa

Written by

My blog as student of Tecnologico de Monterrey

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade