From c956e42492a53a9a5d405ae41c81f7ff60fd1bad Mon Sep 17 00:00:00 2001 From: Karo Mahmoud <116388350+karofmah@users.noreply.github.com> Date: Sun, 15 Oct 2023 18:07:08 +0200 Subject: [PATCH] Platform movement is reversed upon player collision below --- Assets/Scenes/CraterTown.unity | 68 +++++++++++++++++++ Assets/Scripts/Platforms/PlatformCollision.cs | 17 +++++ .../Platforms/PlatformCollision.cs.meta | 11 +++ Assets/Scripts/Platforms/PlatformMovement.cs | 12 ++-- 4 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 Assets/Scripts/Platforms/PlatformCollision.cs create mode 100644 Assets/Scripts/Platforms/PlatformCollision.cs.meta diff --git a/Assets/Scenes/CraterTown.unity b/Assets/Scenes/CraterTown.unity index f0f7aceea..124c0746d 100644 --- a/Assets/Scenes/CraterTown.unity +++ b/Assets/Scenes/CraterTown.unity @@ -3388,6 +3388,7 @@ Transform: - {fileID: 1746097628} - {fileID: 1501441468} - {fileID: 1785816777} + - {fileID: 1041212752} m_Father: {fileID: 0} m_RootOrder: 14 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -3496,6 +3497,73 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 17 m_LocalEulerAnglesHint: {x: 0.892, y: 73.8, z: 26.72} +--- !u!1 &1041212751 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1041212752} + - component: {fileID: 1041212753} + - component: {fileID: 1041212754} + m_Layer: 0 + m_Name: GameObject + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1041212752 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1041212751} + 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: 974049643} + m_RootOrder: -1 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!65 &1041212753 +BoxCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1041212751} + m_Material: {fileID: 0} + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_LayerOverridePriority: 0 + m_IsTrigger: 1 + m_ProvidesContacts: 0 + m_Enabled: 1 + serializedVersion: 3 + m_Size: {x: 3.6315613, y: 0.43898773, z: 3.6156006} + m_Center: {x: 0.0256958, y: -0.8325424, z: 0.008285522} +--- !u!114 &1041212754 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1041212751} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 99aed0618e383a64abfb9827baa90149, type: 3} + m_Name: + m_EditorClassIdentifier: + platformMovement: {fileID: 1746097631} --- !u!1 &1042958923 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Platforms/PlatformCollision.cs b/Assets/Scripts/Platforms/PlatformCollision.cs new file mode 100644 index 000000000..23c619d5f --- /dev/null +++ b/Assets/Scripts/Platforms/PlatformCollision.cs @@ -0,0 +1,17 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +public class PlatformCollision: MonoBehaviour +{ + + [SerializeField] private PlatformMovement platformMovement; + private List startRoutepoints; + private void Start() { + startRoutepoints = platformMovement.routepoints; + } + private void OnTriggerEnter(Collider other) { + if(other.CompareTag("Player")){ + platformMovement.routepoints.Reverse(); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Platforms/PlatformCollision.cs.meta b/Assets/Scripts/Platforms/PlatformCollision.cs.meta new file mode 100644 index 000000000..ccc4d437a --- /dev/null +++ b/Assets/Scripts/Platforms/PlatformCollision.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 99aed0618e383a64abfb9827baa90149 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Platforms/PlatformMovement.cs b/Assets/Scripts/Platforms/PlatformMovement.cs index 30e034537..c2bb2686f 100644 --- a/Assets/Scripts/Platforms/PlatformMovement.cs +++ b/Assets/Scripts/Platforms/PlatformMovement.cs @@ -4,12 +4,18 @@ public class PlatformMovement : MonoBehaviour { -[SerializeField] private List routepoints; +public List routepoints; [SerializeField] private float moveSpeed = 5f; private int nextRoutepointIndex; private float travelDistance; +private Collider sphereCollider; +private Collider meshCollider; + private void Start() { + sphereCollider = GetComponent(); + meshCollider = GetComponent(); + nextRoutepointIndex = 0; travelDistance = Vector3.Distance(routepoints[nextRoutepointIndex + 1].transform.position, transform.position); @@ -38,9 +44,6 @@ private void MovePlatform() { moveSpeed = 2f; - Debug.Log("travel:" + travelDistance); - Debug.Log("current:" + currentDistance); - }else{ moveSpeed = 5f; } @@ -49,6 +52,7 @@ private void MovePlatform() routepoints.Reverse(); nextRoutepointIndex = 0; } + private void OnTriggerEnter(Collider other) { if(other.CompareTag("Player")){ other.transform.SetParent(transform);