From 0b08977787d26ece09f89ede209bb89ea3c7d79c Mon Sep 17 00:00:00 2001 From: Tucan444 Date: Wed, 21 Jul 2021 16:01:32 +0200 Subject: [PATCH 1/2] _ --- Assets/Scripts/MovingPlatform.cs | 102 ++++++++++++++++++++++++++ Assets/Scripts/MovingPlatform.cs.meta | 11 +++ 2 files changed, 113 insertions(+) create mode 100644 Assets/Scripts/MovingPlatform.cs create mode 100644 Assets/Scripts/MovingPlatform.cs.meta diff --git a/Assets/Scripts/MovingPlatform.cs b/Assets/Scripts/MovingPlatform.cs new file mode 100644 index 0000000..eae99f4 --- /dev/null +++ b/Assets/Scripts/MovingPlatform.cs @@ -0,0 +1,102 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class MovingPlatform : MonoBehaviour +{ + + public float width; + public float height; + [Range(0.1f, 8)] [SerializeField] private float speed = 1; + + private bool movingLeft = false; + private bool movingUp = false; + + Vector3 vector; + Vector3 object_position; + Vector3 furthest_position; + + // Start is called before the first frame update + void Start() + { + vector = transform.position; + object_position = transform.position; + furthest_position = object_position; + furthest_position[0] += width; + furthest_position[1] += height; + } + + // Update is called once per frame + void Update() + { + if (width > 0) { + if (movingLeft) { + if (vector[0] > object_position[0]) { + vector[0] -= speed * Time.deltaTime; + } else { + movingLeft = !movingLeft; + vector[0] += speed * Time.deltaTime; + } + } else { + if (vector[0] < furthest_position[0]) { + vector[0] += speed * Time.deltaTime; + } else { + movingLeft = !movingLeft; + vector[0] -= speed * Time.deltaTime; + } + } + } else if (width < 0) { + if (movingLeft) { + if (vector[0] > furthest_position[0]) { + vector[0] -= speed * Time.deltaTime; + } else { + movingLeft = !movingLeft; + vector[0] += speed * Time.deltaTime; + } + } else { + if (vector[0] < object_position[0]) { + vector[0] += speed * Time.deltaTime; + } else { + movingLeft = !movingLeft; + vector[0] -= speed * Time.deltaTime; + } + } + } + + if (height > 0) { + if (movingUp) { + if (vector[1] < furthest_position[1]) { + vector[1] += speed * Time.deltaTime; + } else { + movingUp = !movingUp; + vector[1] -= speed * Time.deltaTime; + } + } else { + if (vector[1] > object_position[1]) { + vector[1] -= speed * Time.deltaTime; + } else { + movingUp = !movingUp; + vector[1] += speed * Time.deltaTime; + } + } + } else if (height < 0) { + if (movingUp) { + if (vector[1] < object_position[1]) { + vector[1] += speed * Time.deltaTime; + } else { + movingUp = !movingUp; + vector[1] -= speed * Time.deltaTime; + } + } else { + if (vector[1] > furthest_position[1]) { + vector[1] -= speed * Time.deltaTime; + } else { + movingUp = !movingUp; + vector[1] += speed * Time.deltaTime; + } + } + } + + transform.position = vector; + } +} diff --git a/Assets/Scripts/MovingPlatform.cs.meta b/Assets/Scripts/MovingPlatform.cs.meta new file mode 100644 index 0000000..d4a2f24 --- /dev/null +++ b/Assets/Scripts/MovingPlatform.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 20c0ecf95ac68c343bdf7b8295ff699c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From 479bbb015ccda33d300d9261b38872283e649009 Mon Sep 17 00:00:00 2001 From: Tucan444 Date: Wed, 21 Jul 2021 16:01:42 +0200 Subject: [PATCH 2/2] _ --- Assets/Scenes/Level1.unity | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Assets/Scenes/Level1.unity b/Assets/Scenes/Level1.unity index dc3a3f0..4b7daee 100644 --- a/Assets/Scenes/Level1.unity +++ b/Assets/Scenes/Level1.unity @@ -135,6 +135,7 @@ GameObject: - component: {fileID: 44549256} - component: {fileID: 44549255} - component: {fileID: 44549254} + - component: {fileID: 44549257} m_Layer: 3 m_Name: Circle m_TagString: Untagged @@ -244,6 +245,21 @@ SpriteRenderer: m_WasSpriteAssigned: 1 m_MaskInteraction: 0 m_SpriteSortPoint: 0 +--- !u!114 &44549257 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 44549252} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 20c0ecf95ac68c343bdf7b8295ff699c, type: 3} + m_Name: + m_EditorClassIdentifier: + width: 0 + height: -1.4 + speed: 1.76 --- !u!1 &48681354 GameObject: m_ObjectHideFlags: 0 @@ -32243,6 +32259,7 @@ GameObject: - component: {fileID: 1657599957} - component: {fileID: 1657599956} - component: {fileID: 1657599955} + - component: {fileID: 1657599958} m_Layer: 3 m_Name: Circle (1) m_TagString: Untagged @@ -32352,6 +32369,21 @@ SpriteRenderer: m_WasSpriteAssigned: 1 m_MaskInteraction: 0 m_SpriteSortPoint: 0 +--- !u!114 &1657599958 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1657599953} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 20c0ecf95ac68c343bdf7b8295ff699c, type: 3} + m_Name: + m_EditorClassIdentifier: + width: 0 + height: -1.36 + speed: 1.2 --- !u!1 &1748848846 GameObject: m_ObjectHideFlags: 0