Commit 19d44d14 authored by Hao Chen (hac185)'s avatar Hao Chen (hac185)
Browse files

modify enemy

parent e3228a8b
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public int startingHealth = 100; // The amount of health the enemy starts the game with.
public int currentHealth =100; // The current health the enemy has.
public float sinkSpeed = 2.5f; // The speed at which the enemy sinks through the floor when dead.
public int scoreValue = 10; // The amount added to the player's score when the enemy dies.
//public AudioClip deathClip; // The sound to play when the enemy dies.
CapsuleCollider capsuleCollider; // Reference to the capsule collider.
bool isDead; // Whether the enemy is dead.
bool isSinking; // Whether the enemy has started sinking through the floor.
void Start(){
//GunController gunCrtl = gun.GetComponent<ProjectileController> ();
}
void Awake ()
{
// Setting up the references.
//anim = GetComponent <Animator> ();
//enemyAudio = GetComponent <AudioSource> ();
//capsuleCollider = GetComponent <CapsuleCollider> ();
// Setting the current health when the enemy first spawns.
currentHealth = startingHealth;
}
void Update ()
{
// If the enemy should be sinking...
if(isSinking)
{
// ... move the enemy down by the sinkSpeed per second.
transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
}
}
public void TakeDamage (int amount, Vector3 hitPoint)
{
Debug.Log("It's working");
// If the enemy is dead...
if (isDead)
{
// ... no need to take damage so exit the function.
Destroy (gameObject);
return;
}
// Reduce the current health by the amount of damage sustained.
currentHealth -= amount;
// If the current health is less than or equal to zero...
if(currentHealth <= 0)
{
// ... the enemy is dead.
Death ();
}
}
void Death ()
{
isDead = true;
}
public void StartSinking ()
{
// Find and disable the Nav Mesh Agent.
GetComponent <NavMeshAgent> ().enabled = false;
// Find the rigidbody component and make it kinematic (since we use Translate to sink the enemy).
GetComponent <Rigidbody> ().isKinematic = true;
// The enemy should no sink.
isSinking = true;
// Increase the score by the enemy's score value.
//ScoreManager.score += scoreValue;
// After 2 seconds destory the enemy.
Destroy (gameObject, 2f);
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 964b099a08d5340f2ac75cda45502047
timeCreated: 1477879287
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using System.Collections;
public class deathOnCollision : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnTriggerEnter2D () {
Die ();
}
void Die(){
Destroy (gameObject);
}
}
fileFormatVersion: 2
guid: c1765c246e55642fe989b5331e523da0
timeCreated: 1477112459
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using System.Collections;
public class enemyScript : MonoBehaviour {
public float speed; //Speed of enemy sprite
public float AttackRange; //range 1: distance to attack from hero
//public float flyingHeight= 5f;
private Transform player; //the position of the hero sprite
public GameObject gun;
public float rateOfFire_sec;
private float nextFireTime_sec;
public float maxSpeed;
private bool isCanFireNow;
private EnemyHealth healthCtrl;
private SpriteRenderer spriteRend;
public Rigidbody2D rb;
void Update(){
if (Time.time > this.nextFireTime_sec) {
this.isCanFireNow = true;
this.nextFireTime_sec = Time.time + this.rateOfFire_sec;
}
if (Vector2.Distance(this.player.transform.position, this.transform.position) < AttackRange && this.isCanFireNow) { //if the enemy is in range, shoot.
//shoot
gun.SendMessage ("Fire");
this.isCanFireNow = false;
}
}
void Start() {
this.nextFireTime_sec = Time.time + rateOfFire_sec;
this.isCanFireNow = true;
GunController gunCrtl = gun.GetComponent<GunController> ();
healthCtrl = GetComponent<EnemyHealth>();
spriteRend = GetComponent<SpriteRenderer> ();
rb = GetComponent <Rigidbody2D> ();
player = GameObject.FindGameObjectWithTag("Player").transform;
}
void FixedUpdate(){
Vector2 facingdirection = player.position - transform.position;
if (this.rb.velocity.magnitude < maxSpeed && Vector3.Distance(transform.position,player.position)> AttackRange) {
//Vector2 force = Vector2.MoveTowards (this.transform.position, player.position, speed);
this.rb.AddForce (facingdirection.normalized * speed);
}
float directionAngle = Mathf.Atan2 (facingdirection.y, facingdirection.x) * Mathf.Rad2Deg;
this.transform.rotation = Quaternion.AngleAxis (directionAngle, Vector3.forward);
if (directionAngle > -90 && directionAngle < 90) {
spriteRend.flipY = false;
}
else {
spriteRend.flipY = true;
}
transform.position = new Vector3(transform.position.x, transform.position.y, 0);
}
void OnCollisionEnter2D(Collision2D other){
if (other.gameObject.CompareTag ("Projectile")) {
//get damage
ProjectileController projCtrl = other.gameObject.GetComponent<ProjectileController> ();
int damage = projCtrl.getDamage ();
healthCtrl.TakeDamage (damage, other.transform.position);
Destroy (other.gameObject);
//Debug.Log ("here");
}
}
// void OnCollisionEnter2D(Collision2D other){
// if (other.CompareTag ("Projectile")) {
// //get damage
// ProjectileController projCtrl = other.gameObject.GetComponent<ProjectileController>();
// int damagae = projCtrl.getDamage ();
// //health script, take damage
// //send message with calling recieveDamage(Will have int damage) within health script
//pass damage to sendMessage, Destroy other
// Destroy(other.gameobject)
}
fileFormatVersion: 2
guid: 7df06c4162d6b4f47b2678a720f172cf
timeCreated: 1477105466
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using System.Collections;
public class movement : MonoBehaviour {
public float movementspeed = 50f;
public Animator animator;
void Start(){
}
// Use this for initialization
void FixedUpdate(){
//Follows mouse and moves sprite towards it
//This was used for my assignment, but I included this here as it was in the same folder as my EnemyMovement
var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Quaternion rot = Quaternion.LookRotation (transform.position - mousePosition, Vector3.back);
transform.rotation = rot;
transform.eulerAngles = new Vector3 (0, 0, transform.eulerAngles.z);
if (Input.GetAxisRaw ("Horizontal") > 0) {
transform.Translate(Vector2.left * Time.deltaTime * movementspeed);
}
else if(Input.GetAxisRaw ("Horizontal") < 0) {
transform.Translate(Vector2.right * Time.deltaTime * movementspeed);
}
if (Input.GetAxisRaw ("Vertical") > 0) {
transform.Translate(Vector2.down * Time.deltaTime * movementspeed);
}
else if (Input.GetAxisRaw ("Vertical") < 0) {
transform.Translate(Vector2.up * Time.deltaTime * movementspeed);
}
}
void Update(){
}
// Update is called once per frame
}
\ No newline at end of file
fileFormatVersion: 2
guid: f31ee4596b91246aca7d498faccbb48c
timeCreated: 1477098841
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using System.Collections;
public class selfDestruct : MonoBehaviour {
public float timer= 1.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
timer -= Time.deltaTime;
if (timer <= 0) {
Destroy (gameObject);
}
}
}
fileFormatVersion: 2
guid: dabe3545dff164fd9b1bf2de54f23757
timeCreated: 1477114072
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
//using UnityEngine;
//using System.Collections;
//
//public class shoot : MonoBehaviour {
//
// public GameObject gun;
//
// //float timeToFire = 0;
//// Transform firePoint;
//
// void Start(){
// GunController gunCrtl = gun.GetComponent<GunController> ();
//
// }
// // Update is called once per frame
// void Update () {
// if (Input.GetKeyDown (KeyCode.Mouse0)) {
// //shoot. Send message
// Debug.Log("bruh");
// gun.SendMessage ("Fire");
//
// }
//
// }
//
//}
\ No newline at end of file
fileFormatVersion: 2
guid: 8012417304054412c93f38a63ed4f98d
timeCreated: 1477108637
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public int startingHealth = 100; // The amount of health the enemy starts the game with.
public int currentHealth =100; // The current health the enemy has.
public float sinkSpeed = 2.5f; // The speed at which the enemy sinks through the floor when dead.
public int scoreValue = 10; // The amount added to the player's score when the enemy dies.
//public AudioClip deathClip; // The sound to play when the enemy dies.
CapsuleCollider capsuleCollider; // Reference to the capsule collider.
bool isDead; // Whether the enemy is dead.
bool isSinking; // Whether the enemy has started sinking through the floor.
void Start(){
//GunController gunCrtl = gun.GetComponent<ProjectileController> ();
}
void Awake ()
{
// Setting up the references.
//anim = GetComponent <Animator> ();
//enemyAudio = GetComponent <AudioSource> ();
//capsuleCollider = GetComponent <CapsuleCollider> ();
// Setting the current health when the enemy first spawns.
currentHealth = startingHealth;
}
void Update ()
{
// If the enemy should be sinking...
if(isSinking)
{
// ... move the enemy down by the sinkSpeed per second.
transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
}
}
public void TakeDamage (int amount, Vector3 hitPoint)
{
Debug.Log("It's working");
// If the enemy is dead...
if (isDead)
{
// ... no need to take damage so exit the function.
Destroy (gameObject);
return;
}
// Reduce the current health by the amount of damage sustained.
currentHealth -= amount;
// If the current health is less than or equal to zero...
if(currentHealth <= 0)
{
// ... the enemy is dead.
Death ();
}
}
void Death ()
{
isDead = true;
}
public void StartSinking ()
{
// Find and disable the Nav Mesh Agent.
GetComponent <NavMeshAgent> ().enabled = false;
// Find the rigidbody component and make it kinematic (since we use Translate to sink the enemy).
GetComponent <Rigidbody> ().isKinematic = true;
// The enemy should no sink.
isSinking = true;
// Increase the score by the enemy's score value.
//ScoreManager.score += scoreValue;
// After 2 seconds destory the enemy.
Destroy (gameObject, 2f);
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 99e86edc72bdd450d8dd29c5e80ff7ab
timeCreated: 1477879287
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using System.Collections;
public class deathOnCollision : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnTriggerEnter2D () {
Die ();
}
void Die(){
Destroy (gameObject);
}
}
fileFormatVersion: 2
guid: afe942f9a75bd4dd4b41f320e02e9812
timeCreated: 1477112459
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using System.Collections;
public class moveForward : MonoBehaviour {
public float maxSpeed = 1.0f;
// Update is called once per frame
void Update () {
Vector3 pos = transform.position;
Vector3 velocity = new Vector3 (0, maxSpeed * Time.deltaTime, 0);
pos += transform.rotation * velocity;
transform.position = pos;
}
}
fileFormatVersion: 2
guid: 643813383713f409fbaad89d9bc5d87e
timeCreated: 1477113475
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment