diff --git a/Assets/Resources/Projectile.prefab b/Assets/Resources/Projectile.prefab new file mode 100644 index 0000000..e4bded6 --- /dev/null +++ b/Assets/Resources/Projectile.prefab @@ -0,0 +1,130 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &346687767969674244 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 7788090778617069880} + - component: {fileID: 2897168716074754190} + - component: {fileID: 4355255703526253332} + - component: {fileID: -1177107984769287179} + m_Layer: 0 + m_Name: Projectile + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &7788090778617069880 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 346687767969674244} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0.5729041, y: 0.03829836, z: 0} + m_LocalScale: {x: 0.15, y: 0.15, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!212 &2897168716074754190 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 346687767969674244} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 0 + m_RayTraceProcedural: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: a97c105638bdf8b4a8650670310a4cd3, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 0 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_Sprite: {fileID: -2413806693520163455, guid: a86470a33a6bf42c4b3595704624658b, type: 3} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 1, y: 1} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 +--- !u!114 &4355255703526253332 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 346687767969674244} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d42fe31ad4964f64587c814da01130b2, type: 3} + m_Name: + m_EditorClassIdentifier: + target: {fileID: 0} + speed: 5 + projectileLifetime: 3 +--- !u!50 &-1177107984769287179 +Rigidbody2D: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 346687767969674244} + m_BodyType: 0 + m_Simulated: 1 + m_UseFullKinematicContacts: 0 + m_UseAutoMass: 0 + m_Mass: 0.2 + m_LinearDrag: 0 + m_AngularDrag: 0.05 + m_GravityScale: 0 + m_Material: {fileID: 0} + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_Interpolate: 0 + m_SleepingMode: 1 + m_CollisionDetection: 0 + m_Constraints: 4 diff --git a/Assets/Resources/Projectile.prefab.meta b/Assets/Resources/Projectile.prefab.meta new file mode 100644 index 0000000..e9e209e --- /dev/null +++ b/Assets/Resources/Projectile.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: ee8f03a43677ce941802d01bbacc2202 +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/MovementTest.unity b/Assets/Scenes/MovementTest.unity index ae90a6f..dde04ab 100644 --- a/Assets/Scenes/MovementTest.unity +++ b/Assets/Scenes/MovementTest.unity @@ -467,6 +467,7 @@ GameObject: - component: {fileID: 220016283} - component: {fileID: 220016282} - component: {fileID: 220016281} + - component: {fileID: 220016285} m_Layer: 0 m_Name: Enemy m_TagString: Untagged @@ -573,9 +574,117 @@ Transform: m_LocalPosition: {x: -10.27, y: 0.64, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 - m_Children: [] + m_Children: + - {fileID: 357638226} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &220016285 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 220016280} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4cdfa33fd6ed7d647a720cbd59cea7df, type: 3} + m_Name: + m_EditorClassIdentifier: + projectile: {fileID: 346687767969674244, guid: ee8f03a43677ce941802d01bbacc2202, type: 3} + Patterns: + - sequence: + - Arc: 30 + Amount: 3 + Speed: 5 + Burst: 0 + Cooldown: 1 + currentPattern: 0 +--- !u!1 &357638225 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 357638226} + - component: {fileID: 357638228} + - component: {fileID: 357638227} + m_Layer: 0 + m_Name: AttackRange + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &357638226 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 357638225} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 220016284} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!58 &357638227 +CircleCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 357638225} + m_Enabled: 1 + m_Density: 1 + m_Material: {fileID: 0} + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_LayerOverridePriority: 0 + m_ForceSendLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_ForceReceiveLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_ContactCaptureLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_CallbackLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_IsTrigger: 1 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: 0, y: 0} + serializedVersion: 2 + m_Radius: 5 +--- !u!114 &357638228 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 357638225} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 7b4220a52291a1140945947ce3f52531, type: 3} + m_Name: + m_EditorClassIdentifier: + range: 5 + playerLayer: + serializedVersion: 2 + m_Bits: 128 + inRange: 0 + target: {fileID: 0} --- !u!1 &646449334 GameObject: m_ObjectHideFlags: 0 @@ -822,7 +931,7 @@ GameObject: - component: {fileID: 970285070} - component: {fileID: 970285069} - component: {fileID: 970285071} - m_Layer: 0 + m_Layer: 7 m_Name: Renderer m_TagString: Untagged m_Icon: {fileID: 0} @@ -1842,9 +1951,9 @@ GameObject: - component: {fileID: 2026065164} - component: {fileID: 2026065163} - component: {fileID: 2026065162} - m_Layer: 0 + m_Layer: 7 m_Name: Player - m_TagString: Untagged + m_TagString: Player m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 diff --git a/Assets/Scripts/Enemy/EnemyProjectile.cs b/Assets/Scripts/Enemy/EnemyProjectile.cs new file mode 100644 index 0000000..e62d6a2 --- /dev/null +++ b/Assets/Scripts/Enemy/EnemyProjectile.cs @@ -0,0 +1,27 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class EnemyProjectile : MonoBehaviour +{ + private GameObject target; + public float speed = 5f; + public float projectileLifetime = 3f; + Rigidbody2D rb; + void Start() + { + rb = GetComponent(); + } + private void Update() + { + target = GetComponent().target; + if (target != null) + FireProjectile(); + } + void FireProjectile() + { + Vector2 directionToTarget = (target.transform.position - transform.position).normalized; + rb.velocity = directionToTarget * speed; + Destroy(this, projectileLifetime); + } +} diff --git a/Assets/Scripts/Enemy/EnemyProjectile.cs.meta b/Assets/Scripts/Enemy/EnemyProjectile.cs.meta new file mode 100644 index 0000000..d46b843 --- /dev/null +++ b/Assets/Scripts/Enemy/EnemyProjectile.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d42fe31ad4964f64587c814da01130b2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Enemy/InstantiateOverTime.cs b/Assets/Scripts/Enemy/InstantiateOverTime.cs new file mode 100644 index 0000000..6cd6b13 --- /dev/null +++ b/Assets/Scripts/Enemy/InstantiateOverTime.cs @@ -0,0 +1,88 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +[System.Serializable] +public class BulletPattern +{ + [SerializeField] + public float Arc = 15; + [SerializeField] + public int Amount = 3; + [SerializeField] + public float Speed = 800; + [SerializeField] + public bool Burst = true; + [SerializeField] + public float Cooldown = 1f; +} + +[System.Serializable] +public struct BulletPatternSequence +{ + [SerializeField] + public List sequence; + [SerializeField] + public int currentPattern; +} + +public class InstantiateOverTime : MonoBehaviour +{ + private float CanShootTime; + + public GameObject projectile; + public List Patterns; + BulletPatternSequence _CurrentSequence; + + private void Start() + { + _CurrentSequence = getRandomPattern(); + } + + private void Update() + { + if (CanShootTime < Time.time) // We are allowed to shoot + { + BulletPattern currentPattern = _CurrentSequence.sequence[_CurrentSequence.currentPattern]; + + // Instantiate boolets for patters + Vector2 arcStart = -transform.up; + + for (int i = 0; i < currentPattern.Amount; i++) + { + float stepSize = currentPattern.Arc / ((float)currentPattern.Amount); + Vector2 cArc = rotate(arcStart, stepSize * i + stepSize / 2f - currentPattern.Arc / 2f); + + GameObject p = Instantiate(projectile, transform.position, Quaternion.identity); + p.transform.LookAt((Vector2)p.transform.position + cArc, new Vector3(0, 0, 1)); + Vector3 r = p.transform.rotation.eulerAngles; + p.transform.rotation = Quaternion.Euler(0, 0, r.z); + p.GetComponent().speed = currentPattern.Speed; + } + + CanShootTime = Time.time + currentPattern.Cooldown; + _CurrentSequence.currentPattern++; + + if (_CurrentSequence.currentPattern >= _CurrentSequence.sequence.Count) + { // Done shooting this sequence + _CurrentSequence = getRandomPattern(); + _CurrentSequence.currentPattern = 0; + } + } + } + + BulletPatternSequence getRandomPattern() + { + return Patterns[Random.Range(0, Patterns.Count)]; + } + + public static Vector2 rotate(Vector2 v, float deg) + { + float delta = deg * (float)Mathf.Deg2Rad; + return new Vector2( + v.x * Mathf.Cos(delta) - v.y * Mathf.Sin(delta), + v.x * Mathf.Sin(delta) + v.y * Mathf.Cos(delta) + ); + } + +} \ No newline at end of file diff --git a/Assets/Scripts/Enemy/InstantiateOverTime.cs.meta b/Assets/Scripts/Enemy/InstantiateOverTime.cs.meta new file mode 100644 index 0000000..fe6485f --- /dev/null +++ b/Assets/Scripts/Enemy/InstantiateOverTime.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4cdfa33fd6ed7d647a720cbd59cea7df +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Enemy/ShootingRange.cs b/Assets/Scripts/Enemy/ShootingRange.cs new file mode 100644 index 0000000..2625d1b --- /dev/null +++ b/Assets/Scripts/Enemy/ShootingRange.cs @@ -0,0 +1,25 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class ShootingRange : MonoBehaviour +{ + public float range = 5f; + public LayerMask playerLayer; + public bool inRange = false; + + public GameObject target; + void Update() + { + } + private void OnTriggerEnter2D(Collider2D collision) + { + if (collision.CompareTag("Player")) + { + inRange = true; + target = collision.GetComponent(); + } + else + inRange = false; + } +} diff --git a/Assets/Scripts/Enemy/ShootingRange.cs.meta b/Assets/Scripts/Enemy/ShootingRange.cs.meta new file mode 100644 index 0000000..40c7a06 --- /dev/null +++ b/Assets/Scripts/Enemy/ShootingRange.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7b4220a52291a1140945947ce3f52531 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: