From 26030084ef3614f33195048f88f69e26fb7d033c Mon Sep 17 00:00:00 2001 From: MartinRemi Date: Mon, 16 Nov 2015 17:26:18 -0500 Subject: [PATCH] Added collision detection with enemies --- Assets/CollisionCheck.cs | 13 ++++++++++++ Assets/CollisionCheck.cs.meta | 12 +++++++++++ Assets/Health.cs | 5 +++++ Assets/Scenes/RemyScene.unity | 27 +++++++++++++++++++++++++ Assets/Scripts/PlayerBehavior.cs | 34 +++++++++++++++++++------------- 5 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 Assets/CollisionCheck.cs create mode 100644 Assets/CollisionCheck.cs.meta diff --git a/Assets/CollisionCheck.cs b/Assets/CollisionCheck.cs new file mode 100644 index 0000000..0f7fd65 --- /dev/null +++ b/Assets/CollisionCheck.cs @@ -0,0 +1,13 @@ +using UnityEngine; +using System.Collections; + +public class CollisionCheck : MonoBehaviour { + + void OnCollisionEnter2D(Collision2D col) + { + if(col.gameObject.name == "Enemy") + { + Debug.Log(gameObject.GetComponent().removeHP(1)); + } + } +} diff --git a/Assets/CollisionCheck.cs.meta b/Assets/CollisionCheck.cs.meta new file mode 100644 index 0000000..2a532f1 --- /dev/null +++ b/Assets/CollisionCheck.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 1a250e41dad412f41a2bdfb3f462c130 +timeCreated: 1447711857 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Health.cs b/Assets/Health.cs index f977773..06b1f0d 100644 --- a/Assets/Health.cs +++ b/Assets/Health.cs @@ -31,4 +31,9 @@ public class Health : MonoBehaviour { { return hp; } + + public void setMaxHP(int amount) + { + maxHP = amount; + } } diff --git a/Assets/Scenes/RemyScene.unity b/Assets/Scenes/RemyScene.unity index 8277228..3fc7034 100644 --- a/Assets/Scenes/RemyScene.unity +++ b/Assets/Scenes/RemyScene.unity @@ -321,6 +321,10 @@ Prefab: propertyPath: m_Constraints value: 4 objectReference: {fileID: 0} + - target: {fileID: 431276, guid: b7819e17682d74b45a71d6831a413e0b, type: 2} + propertyPath: m_RootOrder + value: 0 + objectReference: {fileID: 0} m_RemovedComponents: [] m_ParentPrefab: {fileID: 100100000, guid: b7819e17682d74b45a71d6831a413e0b, type: 2} m_IsPrefabParent: 0 @@ -328,6 +332,29 @@ Prefab: GameObject: m_PrefabParentObject: {fileID: 101678, guid: b7819e17682d74b45a71d6831a413e0b, type: 2} m_PrefabInternal: {fileID: 1024525637} +--- !u!114 &1189818990 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1189818989} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 28110cf2b286c534c94e41ea7e145ef0, type: 3} + m_Name: + m_EditorClassIdentifier: + maxHP: 3 +--- !u!114 &1189818998 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1189818989} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1a250e41dad412f41a2bdfb3f462c130, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &1302261674 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/PlayerBehavior.cs b/Assets/Scripts/PlayerBehavior.cs index a5a6327..976ee45 100644 --- a/Assets/Scripts/PlayerBehavior.cs +++ b/Assets/Scripts/PlayerBehavior.cs @@ -1,7 +1,8 @@ using UnityEngine; using System.Collections; -public class PlayerBehavior : MonoBehaviour { +public class PlayerBehavior : MonoBehaviour +{ public float maxSpeed = 50.0f; @@ -16,27 +17,32 @@ public class PlayerBehavior : MonoBehaviour { private Animator anim; private bool facingRight = true; - // Use this for initialization - void Start () { + // Use this for initialization + void Start() + { rb = gameObject.GetComponent(); anim = gameObject.GetComponent(); - } - - // Update is called once per frame - void Update () { - if(grounded && Input.GetAxis("Vertical") > 0.0f) + } + + // Update is called once per frame + void Update() + { + if (grounded && Input.GetAxis("Vertical") > 0.0f) { anim.SetBool("Ground", false); rb.AddForce(new Vector2(0, jumpForce)); } - //for the shooting animations - if (Input.GetButtonDown ("Fire1")) { - anim.SetBool("Shooting", true); + //for the shooting animations + if (Input.GetButtonDown("Fire1")) + { + anim.SetBool("Shooting", true); - } else { - anim.SetBool ("Shooting", false); - } + } + else + { + anim.SetBool("Shooting", false); + } } void FixedUpdate()