From 54ee7d5467b7c725360448cce1e0af867b046df1 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 7 Nov 2020 21:50:40 +0100 Subject: [PATCH] Checkpoints, rocket ground hits, enemy spawn fix --- Assets/Scenes/SampleScene.unity | 266 +---------------------- Assets/Scripts/EnemyController.cs | 23 +- Assets/Scripts/EnemySpawner.cs | 2 +- Assets/Scripts/OwnCharacterController.cs | 29 ++- Assets/Scripts/PlatformSpawner.cs | 15 +- Assets/Scripts/RocketScript.cs | 6 +- ProjectSettings/TagManager.asset | 1 - 7 files changed, 53 insertions(+), 289 deletions(-) diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 2964803..313b2b6 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -249,7 +249,7 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 14 + m_RootOrder: 10 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!61 &407879046 BoxCollider2D: @@ -357,7 +357,7 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 15 + m_RootOrder: 11 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!60 &413955009 PolygonCollider2D: @@ -587,7 +587,7 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 13 + m_RootOrder: 9 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!60 &887921232 PolygonCollider2D: @@ -614,7 +614,7 @@ PolygonCollider2D: m_AutoTiling: 0 m_Points: m_Paths: - - - {x: -0.28193402, y: 0.33931836} + - - {x: -0.28193402, y: 0.35081562} - {x: -0.3446593, y: 0.30261526} - {x: -0.35751337, y: 0.15566969} - {x: -0.32897726, y: -0.008648053} @@ -735,53 +735,6 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: smoothing: 1 ---- !u!1 &1022878654 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 1022878656} - - component: {fileID: 1022878655} - m_Layer: 0 - m_Name: Checkpoint2 - m_TagString: Checkpoint - m_Icon: {fileID: 4422084297763085224, guid: 0000000000000000d000000000000000, type: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!58 &1022878655 -CircleCollider2D: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1022878654} - m_Enabled: 1 - m_Density: 1 - m_Material: {fileID: 0} - m_IsTrigger: 1 - m_UsedByEffector: 0 - m_UsedByComposite: 0 - m_Offset: {x: 0, y: 0} - serializedVersion: 2 - m_Radius: 1.8256752 ---- !u!4 &1022878656 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1022878654} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 55.62, y: 2.4, z: -2.5798757} - m_LocalScale: {x: 1, y: 1, z: 1} - m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 10 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1043406433 GameObject: m_ObjectHideFlags: 0 @@ -927,88 +880,6 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: smoothing: 1 ---- !u!1 &1208620638 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 1208620640} - - component: {fileID: 1208620639} - m_Layer: 0 - m_Name: tree05.png - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!212 &1208620639 -SpriteRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1208620638} - m_Enabled: 1 - m_CastShadows: 0 - m_ReceiveShadows: 0 - m_DynamicOccludee: 1 - m_MotionVectors: 1 - m_LightProbeUsage: 1 - m_ReflectionProbeUsage: 1 - m_RayTracingMode: 0 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: - - {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} - 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: 8995066611722273982, guid: e9201138fa9192c35b1bbc9745732159, - 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.29, y: 2.3} - m_AdaptiveModeThreshold: 0.5 - m_SpriteTileMode: 0 - m_WasSpriteAssigned: 1 - m_MaskInteraction: 0 - m_SpriteSortPoint: 0 ---- !u!4 &1208620640 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1208620638} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 4.717562, y: 2.3692322, z: 0} - m_LocalScale: {x: 1, y: 1, z: 1} - m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 11 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1351818047 GameObject: m_ObjectHideFlags: 0 @@ -1363,88 +1234,6 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: smoothing: 1 ---- !u!1 &1674767446 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 1674767448} - - component: {fileID: 1674767447} - m_Layer: 0 - m_Name: tree05.png (1) - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!212 &1674767447 -SpriteRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1674767446} - m_Enabled: 1 - m_CastShadows: 0 - m_ReceiveShadows: 0 - m_DynamicOccludee: 1 - m_MotionVectors: 1 - m_LightProbeUsage: 1 - m_ReflectionProbeUsage: 1 - m_RayTracingMode: 0 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: - - {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} - 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: 8995066611722273982, guid: e9201138fa9192c35b1bbc9745732159, - 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.29, y: 2.3} - m_AdaptiveModeThreshold: 0.5 - m_SpriteTileMode: 0 - m_WasSpriteAssigned: 1 - m_MaskInteraction: 0 - m_SpriteSortPoint: 0 ---- !u!4 &1674767448 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1674767446} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 55.63, y: 2.3692322, z: 0} - m_LocalScale: {x: 1, y: 1, z: 1} - m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 12 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1692511763 GameObject: m_ObjectHideFlags: 0 @@ -1475,53 +1264,6 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 6 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!1 &2051666664 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 2051666665} - - component: {fileID: 2051666666} - m_Layer: 0 - m_Name: Checkpoint1 - m_TagString: Checkpoint - m_Icon: {fileID: 4422084297763085224, guid: 0000000000000000d000000000000000, type: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!4 &2051666665 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 2051666664} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 4.73, y: 2.4, z: -2.5798757} - m_LocalScale: {x: 1, y: 1, z: 1} - m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 9 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!58 &2051666666 -CircleCollider2D: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 2051666664} - m_Enabled: 1 - m_Density: 1 - m_Material: {fileID: 0} - m_IsTrigger: 1 - m_UsedByEffector: 0 - m_UsedByComposite: 0 - m_Offset: {x: 0, y: 0} - serializedVersion: 2 - m_Radius: 1.8256752 --- !u!1 &2053847420 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/EnemyController.cs b/Assets/Scripts/EnemyController.cs index c69b761..50d2876 100644 --- a/Assets/Scripts/EnemyController.cs +++ b/Assets/Scripts/EnemyController.cs @@ -32,16 +32,16 @@ public class EnemyController : MonoBehaviour var diff = target.position - tr.position; if (_rb.mass < 0.01f) { //Már lelőttük - if (diff.magnitude > 10) - { //Ha már túl messze van - _rb.velocity = Vector2.zero; - gameObject.SetActive(false); - } + if (diff.magnitude > 10) //Ha már túl messze van + Remove(); _rb.AddForce(new Vector2(0f, flyForce * _rb.mass * _rb.gravityScale)); //Ne maradjon véletlenül útban return; } + if (diff.y > 5) + Remove(); + diff.Normalize(); float sp = ((float) _random.NextDouble() / 2f + 1f) * speed; //1 és 1.5 közötti szorzó _rb.AddForce(diff * (sp * _rb.mass * _rb.gravityScale)); @@ -66,10 +66,7 @@ public class EnemyController : MonoBehaviour { _hitsToRemove--; if (_hitsToRemove == 0) - { - _rb.velocity = Vector2.zero; - gameObject.SetActive(false); - } + Remove(); } public void Hit() @@ -83,9 +80,15 @@ public class EnemyController : MonoBehaviour private void OnCollisionEnter2D(Collision2D other) { var go = other.gameObject; - if(!go.CompareTag("Player")) + if (!go.CompareTag("Player")) return; if (IsAlive()) go.GetComponent().Hit(); } + + private void Remove() + { + _rb.velocity = Vector2.zero; + gameObject.SetActive(false); + } } diff --git a/Assets/Scripts/EnemySpawner.cs b/Assets/Scripts/EnemySpawner.cs index 1d3b4d5..aad60a8 100644 --- a/Assets/Scripts/EnemySpawner.cs +++ b/Assets/Scripts/EnemySpawner.cs @@ -30,7 +30,7 @@ public class EnemySpawner : MonoBehaviour var pos = transform.position + _diff; var enemy = _pool.GetObject(true); if (enemy is null) break; - enemy.transform.position = new Vector3(pos.x, spawnPos.position.y, pos.z); + enemy.transform.position = pos; var rb = enemy.GetComponent(); rb.mass = 1f; rb.gravityScale = 1f; diff --git a/Assets/Scripts/OwnCharacterController.cs b/Assets/Scripts/OwnCharacterController.cs index 8adbb8a..f87f372 100644 --- a/Assets/Scripts/OwnCharacterController.cs +++ b/Assets/Scripts/OwnCharacterController.cs @@ -15,6 +15,8 @@ public class OwnCharacterController : MonoBehaviour private Vector3 _spawnPos; private float _health = 100f; private Random _random = new Random(); + private List _checkpointPosList = new List(); + private Vector3 _checkpointPos; // Start is called before the first frame update void Start() @@ -29,10 +31,10 @@ public class OwnCharacterController : MonoBehaviour if (Mathf.Abs(_rb.velocity.x) > 3) return; float input = Input.GetAxis("Horizontal"); - if (input < 0 && _rb.transform.localScale.x > 0 - || input > 0 && _rb.transform.localScale.x < 0) + var tr = transform; + if (input < 0 && tr.localScale.x > 0 + || input > 0 && tr.localScale.x < 0) { - var tr = transform; var scale = tr.localScale; scale.x *= -1; tr.localScale = scale; @@ -44,6 +46,13 @@ public class OwnCharacterController : MonoBehaviour if (Input.GetButtonDown("Jump") && IsOnGround()) _rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse); + + if (_checkpointPos.x > 0 && (tr.position - _checkpointPos).magnitude < 2f) + { + _spawnPos = _checkpointPos; + _checkpointPosList.RemoveAt(0); + _checkpointPos = _checkpointPosList.Count > 0 ? _checkpointPosList[0] : Vector3.zero; + } } public void Hit() @@ -53,24 +62,22 @@ public class OwnCharacterController : MonoBehaviour Respawn(); } - private void Respawn() + public void Respawn() { transform.position = _spawnPos; _health = 100f; } - private bool IsOnGround() + public bool IsOnGround(string groundName = "") { var res = new List(); _rb.OverlapCollider(new ContactFilter2D(), res); - return res.Any(col => col.CompareTag("Ground")); + return res.Any(col => col.CompareTag("Ground") && (groundName.Length == 0 || col.name.StartsWith(groundName))); } - private void OnTriggerEnter2D(Collider2D other) + public void SetCheckpoint(Vector3 pos) { - var tr = other.transform; - if(!tr.CompareTag("Checkpoint")) - return; - _spawnPos = tr.position; + _checkpointPosList.Add(pos); + if (_checkpointPos.x <= 0) _checkpointPos = _checkpointPosList[0]; } } \ No newline at end of file diff --git a/Assets/Scripts/PlatformSpawner.cs b/Assets/Scripts/PlatformSpawner.cs index dbac7bc..46d97f6 100644 --- a/Assets/Scripts/PlatformSpawner.cs +++ b/Assets/Scripts/PlatformSpawner.cs @@ -15,17 +15,22 @@ public class PlatformSpawner : MonoBehaviour private int _level = 0; private Random _random = new Random(); private Vector3 _lastPlatformPos; + private OwnCharacterController _playerController; + private float _lastLevel0Pos; + // Start is called before the first frame update void Start() { _spawnDiff = platformLeft.position - player.position; _lastPlatformPos = platformRight.position; + _playerController = player.GetComponent(); } // Update is called once per frame void FixedUpdate() { - //If next platform is closer than _spawnDiff + if (_level > 0 && player.position.x > _lastLevel0Pos && _playerController.IsOnGround("Ground")) + _playerController.Respawn(); if (player.position.x + _spawnDiff.x <= _lastPlatformPos.x) return; int size = _random.Next(maxSize); @@ -35,5 +40,13 @@ public class PlatformSpawner : MonoBehaviour Instantiate(platformMiddle).position = pos += new Vector3(0.7f, 0, 0); Instantiate(platformRight).position = pos += new Vector3(0.7f, 0, 0); _lastPlatformPos = pos; + if (_level == 0) + _lastLevel0Pos = pos.x; + if (_random.Next(2) == 1) + { + _level++; + _lastPlatformPos.y++; + _playerController.SetCheckpoint(_lastPlatformPos); + } } } diff --git a/Assets/Scripts/RocketScript.cs b/Assets/Scripts/RocketScript.cs index 625e845..4295b1c 100644 --- a/Assets/Scripts/RocketScript.cs +++ b/Assets/Scripts/RocketScript.cs @@ -40,13 +40,13 @@ public class RocketScript : MonoBehaviour private void OnCollisionEnter2D(Collision2D other) { - if (!other.gameObject.CompareTag("Enemy")) - return; if (_hitCount >= maxHits) gameObject.SetActive(false); _rb.velocity = Vector2.zero; + _hitCount++; + if (!other.gameObject.CompareTag("Enemy")) + return; var ec = other.gameObject.GetComponent(); ec.Hit(); - _hitCount++; } } diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index 74dbb14..10215bb 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -6,7 +6,6 @@ TagManager: tags: - Ground - Enemy - - Checkpoint layers: - Default - TransparentFX