From 7b8222da453611c15e0c97104f4428854de0d30c Mon Sep 17 00:00:00 2001 From: kimrdd Date: Fri, 2 Feb 2024 20:26:17 +0100 Subject: [PATCH 1/3] sync --- Assets/Scenes/MovementTest.unity | 481 +++++++++++++++++++++ Assets/Scripts/Player/PlayerHP.cs | 5 +- ProjectSettings/SceneTemplateSettings.json | 121 ++++++ 3 files changed, 606 insertions(+), 1 deletion(-) create mode 100644 ProjectSettings/SceneTemplateSettings.json diff --git a/Assets/Scenes/MovementTest.unity b/Assets/Scenes/MovementTest.unity index 3bf7535..16769ea 100644 --- a/Assets/Scenes/MovementTest.unity +++ b/Assets/Scenes/MovementTest.unity @@ -215,8 +215,489 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &646449334 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 646449338} + - component: {fileID: 646449337} + - component: {fileID: 646449336} + - component: {fileID: 646449335} + m_Layer: 5 + m_Name: UI + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &646449335 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 646449334} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: dc42784cf147c0c48a680349fa168899, type: 3} + m_Name: + m_EditorClassIdentifier: + m_IgnoreReversedGraphics: 1 + m_BlockingObjects: 0 + m_BlockingMask: + serializedVersion: 2 + m_Bits: 4294967295 +--- !u!114 &646449336 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 646449334} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 0cd44c1031e13a943bb63640046fad76, type: 3} + m_Name: + m_EditorClassIdentifier: + m_UiScaleMode: 0 + m_ReferencePixelsPerUnit: 100 + m_ScaleFactor: 1 + m_ReferenceResolution: {x: 800, y: 600} + m_ScreenMatchMode: 0 + m_MatchWidthOrHeight: 0 + m_PhysicalUnit: 3 + m_FallbackScreenDPI: 96 + m_DefaultSpriteDPI: 96 + m_DynamicPixelsPerUnit: 1 + m_PresetInfoIsWorld: 0 +--- !u!223 &646449337 +Canvas: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 646449334} + m_Enabled: 1 + serializedVersion: 3 + m_RenderMode: 0 + m_Camera: {fileID: 0} + m_PlaneDistance: 100 + m_PixelPerfect: 0 + m_ReceivesEvents: 1 + m_OverrideSorting: 0 + m_OverridePixelPerfect: 0 + m_SortingBucketNormalizedSize: 0 + m_VertexColorAlwaysGammaSpace: 0 + m_AdditionalShaderChannelsFlag: 25 + m_UpdateRectTransformForStandalone: 0 + m_SortingLayerID: 0 + m_SortingOrder: 0 + m_TargetDisplay: 0 +--- !u!224 &646449338 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 646449334} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 0, y: 0, z: 0} + m_ConstrainProportionsScale: 0 + m_Children: + - {fileID: 1666646757} + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 0, y: 0} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 0, y: 0} + m_Pivot: {x: 0, y: 0} +--- !u!1 &970285067 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 970285070} + - component: {fileID: 970285069} + m_Layer: 0 + m_Name: Renderer + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!212 &970285069 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 970285067} + 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!4 &970285070 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 970285067} + serializedVersion: 2 + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0.7082175, y: 0.44170734, z: 0.09089095} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 2026065165} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1420709238 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1420709241} + - component: {fileID: 1420709240} + - component: {fileID: 1420709239} + m_Layer: 0 + m_Name: EventSystem + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1420709239 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1420709238} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4f231c4fb786f3946a6b90b886c48677, type: 3} + m_Name: + m_EditorClassIdentifier: + m_SendPointerHoverToParent: 1 + m_HorizontalAxis: Horizontal + m_VerticalAxis: Vertical + m_SubmitButton: Submit + m_CancelButton: Cancel + m_InputActionsPerSecond: 10 + m_RepeatDelay: 0.5 + m_ForceModuleActive: 0 +--- !u!114 &1420709240 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1420709238} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 76c392e42b5098c458856cdf6ecaaaa1, type: 3} + m_Name: + m_EditorClassIdentifier: + m_FirstSelected: {fileID: 0} + m_sendNavigationEvents: 1 + m_DragThreshold: 10 +--- !u!4 &1420709241 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1420709238} + 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: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1666646756 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1666646757} + - component: {fileID: 1666646759} + - component: {fileID: 1666646758} + m_Layer: 5 + m_Name: Health + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &1666646757 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1666646756} + 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: 646449338} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0.5, y: 0.5} + m_AnchorMax: {x: 0.5, y: 0.5} + m_AnchoredPosition: {x: -657, y: 405} + m_SizeDelta: {x: 200, y: 50} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &1666646758 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1666646756} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_text: New Text + m_isRightToLeft: 0 + m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} + m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2} + m_fontSharedMaterials: [] + m_fontMaterial: {fileID: 0} + m_fontMaterials: [] + m_fontColor32: + serializedVersion: 2 + rgba: 4294967295 + m_fontColor: {r: 1, g: 1, b: 1, a: 1} + m_enableVertexGradient: 0 + m_colorMode: 3 + m_fontColorGradient: + topLeft: {r: 1, g: 1, b: 1, a: 1} + topRight: {r: 1, g: 1, b: 1, a: 1} + bottomLeft: {r: 1, g: 1, b: 1, a: 1} + bottomRight: {r: 1, g: 1, b: 1, a: 1} + m_fontColorGradientPreset: {fileID: 0} + m_spriteAsset: {fileID: 0} + m_tintAllSprites: 0 + m_StyleSheet: {fileID: 0} + m_TextStyleHashCode: -1183493901 + m_overrideHtmlColors: 0 + m_faceColor: + serializedVersion: 2 + rgba: 4294967295 + m_fontSize: 36 + m_fontSizeBase: 36 + m_fontWeight: 400 + m_enableAutoSizing: 0 + m_fontSizeMin: 18 + m_fontSizeMax: 72 + m_fontStyle: 0 + m_HorizontalAlignment: 1 + m_VerticalAlignment: 256 + m_textAlignment: 65535 + m_characterSpacing: 0 + m_wordSpacing: 0 + m_lineSpacing: 0 + m_lineSpacingMax: 0 + m_paragraphSpacing: 0 + m_charWidthMaxAdj: 0 + m_enableWordWrapping: 1 + m_wordWrappingRatios: 0.4 + m_overflowMode: 0 + m_linkedTextComponent: {fileID: 0} + parentLinkedComponent: {fileID: 0} + m_enableKerning: 1 + m_enableExtraPadding: 0 + checkPaddingRequired: 0 + m_isRichText: 1 + m_parseCtrlCharacters: 1 + m_isOrthographic: 1 + m_isCullingEnabled: 0 + m_horizontalMapping: 0 + m_verticalMapping: 0 + m_uvLineOffset: 0 + m_geometrySortingOrder: 0 + m_IsTextObjectScaleStatic: 0 + m_VertexBufferAutoSizeReduction: 0 + m_useMaxVisibleDescender: 1 + m_pageToDisplay: 1 + m_margin: {x: 0, y: 0, z: 0, w: 0} + m_isUsingLegacyAnimationComponent: 0 + m_isVolumetricText: 0 + m_hasFontAssetChanged: 0 + m_baseMaterial: {fileID: 0} + m_maskOffset: {x: 0, y: 0, z: 0, w: 0} +--- !u!222 &1666646759 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1666646756} + m_CullTransparentMesh: 1 +--- !u!1 &2026065161 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 2026065165} + - component: {fileID: 2026065164} + - component: {fileID: 2026065163} + - component: {fileID: 2026065162} + m_Layer: 0 + m_Name: Player + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!50 &2026065162 +Rigidbody2D: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2026065161} + m_BodyType: 0 + m_Simulated: 1 + m_UseFullKinematicContacts: 0 + m_UseAutoMass: 0 + m_Mass: 1 + 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: 1 + m_Constraints: 0 +--- !u!114 &2026065163 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2026065161} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1fdfc885f1a69704b893825cf9e616ba, type: 3} + m_Name: + m_EditorClassIdentifier: + maxHealth: 100 + currentHealth: 0 + healthText: {fileID: 1666646758} +--- !u!114 &2026065164 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2026065161} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 3dbdb849a2f5df14d9e109a7776c5ac0, type: 3} + m_Name: + m_EditorClassIdentifier: + moveSpeed: 5 +--- !u!4 &2026065165 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2026065161} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -0.7082175, y: -0.44170734, z: -0.09089095} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: + - {fileID: 970285070} + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1660057539 &9223372036854775807 SceneRoots: m_ObjectHideFlags: 0 m_Roots: - {fileID: 63829549} + - {fileID: 2026065165} + - {fileID: 646449338} + - {fileID: 1420709241} diff --git a/Assets/Scripts/Player/PlayerHP.cs b/Assets/Scripts/Player/PlayerHP.cs index 02b2c04..9c70ec6 100644 --- a/Assets/Scripts/Player/PlayerHP.cs +++ b/Assets/Scripts/Player/PlayerHP.cs @@ -24,7 +24,10 @@ public class PlayerHP : MonoBehaviour healthText.text = "Health: " + currentHealth; } else if (healthText != null && currentHealth <= 0) - healthText.text = "Health: " + 0; + healthText.text = "Health: 0"; + + if (Input.GetKeyDown(KeyCode.Space)) + TakeDamage(5); } public void TakeDamage(float damage) diff --git a/ProjectSettings/SceneTemplateSettings.json b/ProjectSettings/SceneTemplateSettings.json new file mode 100644 index 0000000..5e97f83 --- /dev/null +++ b/ProjectSettings/SceneTemplateSettings.json @@ -0,0 +1,121 @@ +{ + "templatePinStates": [], + "dependencyTypeInfos": [ + { + "userAdded": false, + "type": "UnityEngine.AnimationClip", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEditor.Animations.AnimatorController", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEngine.AnimatorOverrideController", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEditor.Audio.AudioMixerController", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEngine.ComputeShader", + "defaultInstantiationMode": 1 + }, + { + "userAdded": false, + "type": "UnityEngine.Cubemap", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEngine.GameObject", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEditor.LightingDataAsset", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEngine.LightingSettings", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEngine.Material", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEditor.MonoScript", + "defaultInstantiationMode": 1 + }, + { + "userAdded": false, + "type": "UnityEngine.PhysicMaterial", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEngine.PhysicsMaterial2D", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEngine.Rendering.PostProcessing.PostProcessProfile", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEngine.Rendering.PostProcessing.PostProcessResources", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEngine.Rendering.VolumeProfile", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEditor.SceneAsset", + "defaultInstantiationMode": 1 + }, + { + "userAdded": false, + "type": "UnityEngine.Shader", + "defaultInstantiationMode": 1 + }, + { + "userAdded": false, + "type": "UnityEngine.ShaderVariantCollection", + "defaultInstantiationMode": 1 + }, + { + "userAdded": false, + "type": "UnityEngine.Texture", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEngine.Texture2D", + "defaultInstantiationMode": 0 + }, + { + "userAdded": false, + "type": "UnityEngine.Timeline.TimelineAsset", + "defaultInstantiationMode": 0 + } + ], + "defaultDependencyTypeInfo": { + "userAdded": false, + "type": "", + "defaultInstantiationMode": 1 + }, + "newSceneOverride": 0 +} \ No newline at end of file From 692cbf4d1058b80cbff55fe2953d1db330095992 Mon Sep 17 00:00:00 2001 From: Sveske Juice Date: Fri, 2 Feb 2024 12:03:21 -0800 Subject: [PATCH 2/3] extensions --- Assets/Scenes/RopeTest.unity | 271 ++++++++++++++++++ Assets/Scenes/RopeTest.unity.meta | 7 + .../Extensions/GameObjectExtensions.cs | 131 +++++++++ .../Extensions/GameObjectExtensions.cs.meta | 11 + .../Scripts/Extensions/TransformExtensions.cs | 98 +++++++ .../Extensions/TransformExtensions.cs.meta | 11 + .../Scripts/Extensions/Vector2Extensions.cs | 35 +++ .../Extensions/Vector2Extensions.cs.meta | 11 + .../Scripts/Extensions/Vector3Extensions.cs | 60 ++++ .../Extensions/Vector3Extensions.cs.meta | 11 + ProjectSettings/ShaderGraphSettings.asset | 17 ++ 11 files changed, 663 insertions(+) create mode 100644 Assets/Scenes/RopeTest.unity create mode 100644 Assets/Scenes/RopeTest.unity.meta create mode 100644 Assets/Scripts/Extensions/GameObjectExtensions.cs create mode 100644 Assets/Scripts/Extensions/GameObjectExtensions.cs.meta create mode 100644 Assets/Scripts/Extensions/TransformExtensions.cs create mode 100644 Assets/Scripts/Extensions/TransformExtensions.cs.meta create mode 100644 Assets/Scripts/Extensions/Vector2Extensions.cs create mode 100644 Assets/Scripts/Extensions/Vector2Extensions.cs.meta create mode 100644 Assets/Scripts/Extensions/Vector3Extensions.cs create mode 100644 Assets/Scripts/Extensions/Vector3Extensions.cs.meta create mode 100644 ProjectSettings/ShaderGraphSettings.asset diff --git a/Assets/Scenes/RopeTest.unity b/Assets/Scenes/RopeTest.unity new file mode 100644 index 0000000..8a451de --- /dev/null +++ b/Assets/Scenes/RopeTest.unity @@ -0,0 +1,271 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!29 &1 +OcclusionCullingSettings: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_OcclusionBakeSettings: + smallestOccluder: 5 + smallestHole: 0.25 + backfaceThreshold: 100 + m_SceneGUID: 00000000000000000000000000000000 + m_OcclusionCullingData: {fileID: 0} +--- !u!104 &2 +RenderSettings: + m_ObjectHideFlags: 0 + serializedVersion: 9 + m_Fog: 0 + m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} + m_FogMode: 3 + m_FogDensity: 0.01 + m_LinearFogStart: 0 + m_LinearFogEnd: 300 + m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1} + m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} + m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} + m_AmbientIntensity: 1 + m_AmbientMode: 3 + m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} + m_SkyboxMaterial: {fileID: 0} + m_HaloStrength: 0.5 + m_FlareStrength: 1 + m_FlareFadeSpeed: 3 + m_HaloTexture: {fileID: 0} + m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} + m_DefaultReflectionMode: 0 + m_DefaultReflectionResolution: 128 + m_ReflectionBounces: 1 + m_ReflectionIntensity: 1 + m_CustomReflection: {fileID: 0} + m_Sun: {fileID: 0} + m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1} + m_UseRadianceAmbientProbe: 0 +--- !u!157 &3 +LightmapSettings: + m_ObjectHideFlags: 0 + serializedVersion: 12 + m_GIWorkflowMode: 1 + m_GISettings: + serializedVersion: 2 + m_BounceScale: 1 + m_IndirectOutputScale: 1 + m_AlbedoBoost: 1 + m_EnvironmentLightingMode: 0 + m_EnableBakedLightmaps: 0 + m_EnableRealtimeLightmaps: 0 + m_LightmapEditorSettings: + serializedVersion: 12 + m_Resolution: 2 + m_BakeResolution: 40 + m_AtlasSize: 1024 + m_AO: 0 + m_AOMaxDistance: 1 + m_CompAOExponent: 1 + m_CompAOExponentDirect: 0 + m_ExtractAmbientOcclusion: 0 + m_Padding: 2 + m_LightmapParameters: {fileID: 0} + m_LightmapsBakeMode: 1 + m_TextureCompression: 1 + m_FinalGather: 0 + m_FinalGatherFiltering: 1 + m_FinalGatherRayCount: 256 + m_ReflectionCompression: 2 + m_MixedBakeMode: 2 + m_BakeBackend: 1 + m_PVRSampling: 1 + m_PVRDirectSampleCount: 32 + m_PVRSampleCount: 512 + m_PVRBounces: 2 + m_PVREnvironmentSampleCount: 256 + m_PVREnvironmentReferencePointCount: 2048 + m_PVRFilteringMode: 1 + m_PVRDenoiserTypeDirect: 1 + m_PVRDenoiserTypeIndirect: 1 + m_PVRDenoiserTypeAO: 1 + m_PVRFilterTypeDirect: 0 + m_PVRFilterTypeIndirect: 0 + m_PVRFilterTypeAO: 0 + m_PVREnvironmentMIS: 1 + m_PVRCulling: 1 + m_PVRFilteringGaussRadiusDirect: 1 + m_PVRFilteringGaussRadiusIndirect: 5 + m_PVRFilteringGaussRadiusAO: 2 + m_PVRFilteringAtrousPositionSigmaDirect: 0.5 + m_PVRFilteringAtrousPositionSigmaIndirect: 2 + m_PVRFilteringAtrousPositionSigmaAO: 1 + m_ExportTrainingData: 0 + m_TrainingDataDestination: TrainingData + m_LightProbeSampleCountMultiplier: 4 + m_LightingDataAsset: {fileID: 0} + m_LightingSettings: {fileID: 0} +--- !u!196 &4 +NavMeshSettings: + serializedVersion: 2 + m_ObjectHideFlags: 0 + m_BuildSettings: + serializedVersion: 3 + agentTypeID: 0 + agentRadius: 0.5 + agentHeight: 2 + agentSlope: 45 + agentClimb: 0.4 + ledgeDropHeight: 0 + maxJumpAcrossDistance: 0 + minRegionArea: 2 + manualCellSize: 0 + cellSize: 0.16666667 + manualTileSize: 0 + tileSize: 256 + buildHeightMesh: 0 + maxJobWorkers: 0 + preserveTilesOutsideBounds: 0 + debug: + m_Flags: 0 + m_NavMeshData: {fileID: 0} +--- !u!1 &22879098 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 22879099} + - component: {fileID: 22879100} + m_Layer: 0 + m_Name: RopeSImulator + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &22879099 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 22879098} + 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: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &22879100 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 22879098} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 27ac133d9e10e544ba603e07122e3359, type: 3} + m_Name: + m_EditorClassIdentifier: + gravity: 5 + solveIterations: 10 + constrainStickMinLength: 0.1 + testPos: {x: 0, y: 0} +--- !u!1 &1993214202 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1993214205} + - component: {fileID: 1993214204} + - component: {fileID: 1993214203} + m_Layer: 0 + m_Name: Main Camera + m_TagString: MainCamera + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!81 &1993214203 +AudioListener: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1993214202} + m_Enabled: 1 +--- !u!20 &1993214204 +Camera: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1993214202} + m_Enabled: 1 + serializedVersion: 2 + m_ClearFlags: 1 + m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0} + m_projectionMatrixMode: 1 + m_GateFitMode: 2 + m_FOVAxisMode: 0 + m_Iso: 200 + m_ShutterSpeed: 0.005 + m_Aperture: 16 + m_FocusDistance: 10 + m_FocalLength: 50 + m_BladeCount: 5 + m_Curvature: {x: 2, y: 11} + m_BarrelClipping: 0.25 + m_Anamorphism: 0 + m_SensorSize: {x: 36, y: 24} + m_LensShift: {x: 0, y: 0} + m_NormalizedViewPortRect: + serializedVersion: 2 + x: 0 + y: 0 + width: 1 + height: 1 + near clip plane: 0.3 + far clip plane: 1000 + field of view: 60 + orthographic: 1 + orthographic size: 5 + m_Depth: -1 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingPath: -1 + m_TargetTexture: {fileID: 0} + m_TargetDisplay: 0 + m_TargetEye: 3 + m_HDR: 1 + m_AllowMSAA: 1 + m_AllowDynamicResolution: 0 + m_ForceIntoRT: 0 + m_OcclusionCulling: 1 + m_StereoConvergence: 10 + m_StereoSeparation: 0.022 +--- !u!4 &1993214205 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1993214202} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: -10} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1660057539 &9223372036854775807 +SceneRoots: + m_ObjectHideFlags: 0 + m_Roots: + - {fileID: 1993214205} + - {fileID: 22879099} diff --git a/Assets/Scenes/RopeTest.unity.meta b/Assets/Scenes/RopeTest.unity.meta new file mode 100644 index 0000000..41c5dd9 --- /dev/null +++ b/Assets/Scenes/RopeTest.unity.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: cfc0abf7f9f0d774f918832b04042f61 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Extensions/GameObjectExtensions.cs b/Assets/Scripts/Extensions/GameObjectExtensions.cs new file mode 100644 index 0000000..74d8e75 --- /dev/null +++ b/Assets/Scripts/Extensions/GameObjectExtensions.cs @@ -0,0 +1,131 @@ +using UnityEngine; +using System.Linq; + +namespace UnityUtils +{ + public static class GameObjectExtensions + { + /// + /// This method is used to hide the GameObject in the Hierarchy view. + /// + /// + public static void HideInHierarchy(this GameObject gameObject) + { + gameObject.hideFlags = HideFlags.HideInHierarchy; + } + + /// + /// Gets a component of the given type attached to the GameObject. If that type of component does not exist, it adds one. + /// + /// + /// This method is useful when you don't know if a GameObject has a specific type of component, + /// but you want to work with that component regardless. Instead of checking and adding the component manually, + /// you can use this method to do both operations in one line. + /// + /// The type of the component to get or add. + /// The GameObject to get the component from or add the component to. + /// The existing component of the given type, or a new one if no such component exists. + public static T GetOrAdd(this GameObject gameObject) where T : Component + { + T component = gameObject.GetComponent(); + if (!component) component = gameObject.AddComponent(); + + return component; + } + + /// + /// Returns the object itself if it exists, null otherwise. + /// + /// + /// This method helps differentiate between a null reference and a destroyed Unity object. Unity's "== null" check + /// can incorrectly return true for destroyed objects, leading to misleading behaviour. The OrNull method use + /// Unity's "null check", and if the object has been marked for destruction, it ensures an actual null reference is returned, + /// aiding in correctly chaining operations and preventing NullReferenceExceptions. + /// + /// The type of the object. + /// The object being checked. + /// The object itself if it exists and not destroyed, null otherwise. + public static T OrNull(this T obj) where T : Object => obj ? obj : null; + + /// + /// Destroys all children of the game object + /// + /// GameObject whose children are to be destroyed. + public static void DestroyChildren(this GameObject gameObject) + { + gameObject.transform.DestroyChildren(); + } + + /// + /// Immediately destroys all children of the given GameObject. + /// + /// GameObject whose children are to be destroyed. + public static void DestroyChildrenImmediate(this GameObject gameObject) + { + gameObject.transform.DestroyChildrenImmediate(); + } + + /// + /// Enables all child GameObjects associated with the given GameObject. + /// + /// GameObject whose child GameObjects are to be enabled. + public static void EnableChildren(this GameObject gameObject) + { + gameObject.transform.EnableChildren(); + } + + /// + /// Disables all child GameObjects associated with the given GameObject. + /// + /// GameObject whose child GameObjects are to be disabled. + public static void DisableChildren(this GameObject gameObject) + { + gameObject.transform.DisableChildren(); + } + + /// + /// Resets the GameObject's transform's position, rotation, and scale to their default values. + /// + /// GameObject whose transformation is to be reset. + public static void ResetTransformation(this GameObject gameObject) + { + gameObject.transform.Reset(); + } + + /// + /// Returns the hierarchical path in the Unity scene hierarchy for this GameObject. + /// + /// The GameObject to get the path for. + /// A string representing the full hierarchical path of this GameObject in the Unity scene. + /// This is a '/'-separated string where each part is the name of a parent, starting from the root parent and ending + /// with the name of the specified GameObjects parent. + public static string Path(this GameObject gameObject) + { + return "/" + string.Join("/", + gameObject.GetComponentsInParent().Select(t => t.name).Reverse().ToArray()); + } + + /// + /// Returns the full hierarchical path in the Unity scene hierarchy for this GameObject. + /// + /// The GameObject to get the path for. + /// A string representing the full hierarchical path of this GameObject in the Unity scene. + /// This is a '/'-separated string where each part is the name of a parent, starting from the root parent and ending + /// with the name of the specified GameObject itself. + public static string PathFull(this GameObject gameObject) + { + return gameObject.Path() + "/" + gameObject.name; + } + + /// + /// Recursively sets the provided layer for this GameObject and all of its descendants in the Unity scene hierarchy. + /// + /// The GameObject to set layers for. + /// The layer number to set for GameObject and all of its descendants. + public static void SetLayersRecursively(this GameObject gameObject, int layer) + { + gameObject.layer = layer; + gameObject.transform.ForEveryChild(child => child.gameObject.SetLayersRecursively(layer)); + } + } +} diff --git a/Assets/Scripts/Extensions/GameObjectExtensions.cs.meta b/Assets/Scripts/Extensions/GameObjectExtensions.cs.meta new file mode 100644 index 0000000..8d4cc67 --- /dev/null +++ b/Assets/Scripts/Extensions/GameObjectExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1fc386696b14b914cbae26f3c3a51a5a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Extensions/TransformExtensions.cs b/Assets/Scripts/Extensions/TransformExtensions.cs new file mode 100644 index 0000000..ebdb88c --- /dev/null +++ b/Assets/Scripts/Extensions/TransformExtensions.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using UnityEngine; +using Object = UnityEngine.Object; + +namespace UnityUtils +{ + public static class TransformExtensions + { + /// + /// Retrieves all the children of a given Transform. + /// + /// + /// This method can be used with LINQ to perform operations on all child Transforms. For example, + /// you could use it to find all children with a specific tag, to disable all children, etc. + /// Transform implements IEnumerable and the GetEnumerator method which returns an IEnumerator of all its children. + /// + /// The Transform to retrieve children from. + /// An IEnumerable<Transform> containing all the child Transforms of the parent. + public static IEnumerable Children(this Transform parent) + { + foreach (Transform child in parent) + { + yield return child; + } + } + + /// + /// Resets transform's position, scale and rotation + /// + /// Transform to use + public static void Reset(this Transform transform) + { + transform.position = Vector3.zero; + transform.localRotation = Quaternion.identity; + transform.localScale = Vector3.one; + } + + /// + /// Destroys all child game objects of the given transform. + /// + /// The Transform whose child game objects are to be destroyed. + public static void DestroyChildren(this Transform parent) + { + parent.ForEveryChild(child => Object.Destroy(child.gameObject)); + } + + /// + /// Immediately destroys all child game objects of the given transform. + /// + /// The Transform whose child game objects are to be immediately destroyed. + public static void DestroyChildrenImmediate(this Transform parent) + { + parent.ForEveryChild(child => Object.DestroyImmediate(child.gameObject)); + } + + /// + /// Enables all child game objects of the given transform. + /// + /// The Transform whose child game objects are to be enabled. + public static void EnableChildren(this Transform parent) + { + parent.ForEveryChild(child => child.gameObject.SetActive(true)); + } + + /// + /// Disables all child game objects of the given transform. + /// + /// The Transform whose child game objects are to be disabled. + public static void DisableChildren(this Transform parent) + { + parent.ForEveryChild(child => child.gameObject.SetActive(false)); + } + + /// + /// Executes a specified action for each child of a given transform. + /// + /// The parent transform. + /// The action to be performed on each child. + /// + /// This method iterates over all child transforms in reverse order and executes a given action on them. + /// The action is a delegate that takes a Transform as parameter. + /// + public static void ForEveryChild(this Transform parent, System.Action action) + { + for (var i = parent.childCount - 1; i >= 0; i--) + { + action(parent.GetChild(i)); + } + } + + [Obsolete("Renamed to ForEveryChild")] + static void PerformActionOnChildren(this Transform parent, System.Action action) + { + parent.ForEveryChild(action); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/TransformExtensions.cs.meta b/Assets/Scripts/Extensions/TransformExtensions.cs.meta new file mode 100644 index 0000000..da47cb8 --- /dev/null +++ b/Assets/Scripts/Extensions/TransformExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3bc029dcadc3ccd40ad4c6a3f51eac64 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Extensions/Vector2Extensions.cs b/Assets/Scripts/Extensions/Vector2Extensions.cs new file mode 100644 index 0000000..8b98d2f --- /dev/null +++ b/Assets/Scripts/Extensions/Vector2Extensions.cs @@ -0,0 +1,35 @@ +using UnityEngine; + +namespace UnityUtils +{ + public static class Vector2Extensions + { + /// + /// Adds to any x y values of a Vector2 + /// + public static Vector2 Add(this Vector2 vector2, float x = 0, float y = 0) + { + return new Vector2(vector2.x + x, vector2.y + y); + } + + /// + /// Sets any x y values of a Vector2 + /// + public static Vector2 With(this Vector2 vector2, float? x = null, float? y = null) + { + return new Vector2(x ?? vector2.x, y ?? vector2.y); + } + + /// + /// Returns a Boolean indicating whether the current Vector2 is in a given range from another Vector2 + /// + /// The current Vector2 position + /// The Vector2 position to compare against + /// The range value to compare against + /// True if the current Vector2 is in the given range from the target Vector2, false otherwise + public static bool InRangeOf(this Vector2 current, Vector2 target, float range) + { + return (current - target).sqrMagnitude <= range * range; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions/Vector2Extensions.cs.meta b/Assets/Scripts/Extensions/Vector2Extensions.cs.meta new file mode 100644 index 0000000..8e79e31 --- /dev/null +++ b/Assets/Scripts/Extensions/Vector2Extensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 69ebf1c43ff162f488b3b1d0ae557d78 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Extensions/Vector3Extensions.cs b/Assets/Scripts/Extensions/Vector3Extensions.cs new file mode 100644 index 0000000..b5c3fe0 --- /dev/null +++ b/Assets/Scripts/Extensions/Vector3Extensions.cs @@ -0,0 +1,60 @@ +using UnityEngine; + +namespace UnityUtils +{ + public static class Vector3Extensions + { + /// + /// Sets any x y z values of a Vector3 + /// + public static Vector3 With(this Vector3 vector, float? x = null, float? y = null, float? z = null) + { + return new Vector3(x ?? vector.x, y ?? vector.y, z ?? vector.z); + } + + /// + /// Adds to any x y z values of a Vector3 + /// + public static Vector3 Add(this Vector3 vector, float x = 0, float y = 0, float z = 0) + { + return new Vector3(vector.x + x, vector.y + y, vector.z + z); + } + + /// + /// Returns a Boolean indicating whether the current Vector3 is in a given range from another Vector3 + /// + /// The current Vector3 position + /// The Vector3 position to compare against + /// The range value to compare against + /// True if the current Vector3 is in the given range from the target Vector3, false otherwise + public static bool InRangeOf(this Vector3 current, Vector3 target, float range) + { + return (current - target).sqrMagnitude <= range * range; + } + + /// + /// Divides two Vector3 objects component-wise. + /// + /// + /// For each component in v0 (x, y, z), it is divided by the corresponding component in v1 if the component in v1 is not zero. + /// Otherwise, the component in v0 remains unchanged. + /// + /// + /// Use 'ComponentDivide' to scale a game object proportionally: + /// + /// myObject.transform.localScale = originalScale.ComponentDivide(targetDimensions); + /// + /// This scales the object size to fit within the target dimensions while maintaining its original proportions. + /// + /// The Vector3 object that this method extends. + /// The Vector3 object by which v0 is divided. + /// A new Vector3 object resulting from the component-wise division. + public static Vector3 ComponentDivide(this Vector3 v0, Vector3 v1) + { + return new Vector3( + v1.x != 0 ? v0.x / v1.x : v0.x, + v1.y != 0 ? v0.y / v1.y : v0.y, + v1.z != 0 ? v0.z / v1.z : v0.z); + } + } +} diff --git a/Assets/Scripts/Extensions/Vector3Extensions.cs.meta b/Assets/Scripts/Extensions/Vector3Extensions.cs.meta new file mode 100644 index 0000000..2882baf --- /dev/null +++ b/Assets/Scripts/Extensions/Vector3Extensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fde3957705d25994292dd59ab09ce951 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ProjectSettings/ShaderGraphSettings.asset b/ProjectSettings/ShaderGraphSettings.asset new file mode 100644 index 0000000..9b43acd --- /dev/null +++ b/ProjectSettings/ShaderGraphSettings.asset @@ -0,0 +1,17 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &1 +MonoBehaviour: + m_ObjectHideFlags: 53 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: de02f9e1d18f588468e474319d09a723, type: 3} + m_Name: + m_EditorClassIdentifier: + shaderVariantLimit: 128 + customInterpolatorErrorThreshold: 32 + customInterpolatorWarningThreshold: 16 From 1cbff2800f000e88bf7b55216d56305ca0cfbf2b Mon Sep 17 00:00:00 2001 From: Sveske Juice Date: Fri, 2 Feb 2024 12:03:44 -0800 Subject: [PATCH 3/3] rope start --- Assets/Scripts/Rope/Point.cs | 15 +++ Assets/Scripts/Rope/Point.cs.meta | 11 ++ Assets/Scripts/Rope/Rope.cs | 14 +++ Assets/Scripts/Rope/Rope.cs.meta | 11 ++ Assets/Scripts/Rope/RopeBuilder.cs | 32 ++++++ Assets/Scripts/Rope/RopeBuilder.cs.meta | 11 ++ Assets/Scripts/Rope/RopeSimulator.cs | 129 ++++++++++++++++++++++ Assets/Scripts/Rope/RopeSimulator.cs.meta | 11 ++ Assets/Scripts/Rope/Stick.cs | 16 +++ Assets/Scripts/Rope/Stick.cs.meta | 11 ++ 10 files changed, 261 insertions(+) create mode 100644 Assets/Scripts/Rope/Point.cs create mode 100644 Assets/Scripts/Rope/Point.cs.meta create mode 100644 Assets/Scripts/Rope/Rope.cs create mode 100644 Assets/Scripts/Rope/Rope.cs.meta create mode 100644 Assets/Scripts/Rope/RopeBuilder.cs create mode 100644 Assets/Scripts/Rope/RopeBuilder.cs.meta create mode 100644 Assets/Scripts/Rope/RopeSimulator.cs create mode 100644 Assets/Scripts/Rope/RopeSimulator.cs.meta create mode 100644 Assets/Scripts/Rope/Stick.cs create mode 100644 Assets/Scripts/Rope/Stick.cs.meta diff --git a/Assets/Scripts/Rope/Point.cs b/Assets/Scripts/Rope/Point.cs new file mode 100644 index 0000000..65a928d --- /dev/null +++ b/Assets/Scripts/Rope/Point.cs @@ -0,0 +1,15 @@ +using UnityEngine; + +[System.Serializable] +public class Point +{ + public Vector2 position, prevPosition; + public bool locked; + + public Point(Vector2 position, bool locked = false) + { + this.position = position; + this.prevPosition = position; + this.locked = locked; + } +} \ No newline at end of file diff --git a/Assets/Scripts/Rope/Point.cs.meta b/Assets/Scripts/Rope/Point.cs.meta new file mode 100644 index 0000000..dc66062 --- /dev/null +++ b/Assets/Scripts/Rope/Point.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a07dfdaccf053414085b41179c0ab234 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Rope/Rope.cs b/Assets/Scripts/Rope/Rope.cs new file mode 100644 index 0000000..de4d20b --- /dev/null +++ b/Assets/Scripts/Rope/Rope.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using UnityEngine; + +public class Rope +{ + public List points { get ; private set; } + public List sticks { get; private set; } + + public Rope(List points, List sticks) + { + this.points = points; + this.sticks = sticks; + } +} diff --git a/Assets/Scripts/Rope/Rope.cs.meta b/Assets/Scripts/Rope/Rope.cs.meta new file mode 100644 index 0000000..6b679cf --- /dev/null +++ b/Assets/Scripts/Rope/Rope.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fb42a2c8fa09c32489663226dc7167e1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Rope/RopeBuilder.cs b/Assets/Scripts/Rope/RopeBuilder.cs new file mode 100644 index 0000000..2de53aa --- /dev/null +++ b/Assets/Scripts/Rope/RopeBuilder.cs @@ -0,0 +1,32 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class RopeBuilder +{ + List points = new(); + List sticks = new(); + + public RopeBuilder AddPoint(Point point) + { + points.Add(point); + return this; + } + + public RopeBuilder ConnectPoints(Point A, Point B) + { + sticks.Add(new Stick(A, B)); + return this; + } + + public RopeBuilder ConnectPoints(int idxA, int idxB) + { + sticks.Add(new Stick(points[idxA], points[idxB])); + return this; + } + + public Rope Build() + { + return new Rope(points: points, sticks: sticks); + } +} diff --git a/Assets/Scripts/Rope/RopeBuilder.cs.meta b/Assets/Scripts/Rope/RopeBuilder.cs.meta new file mode 100644 index 0000000..d98389a --- /dev/null +++ b/Assets/Scripts/Rope/RopeBuilder.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e87b5185694f23a4390596cc0294bee7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Rope/RopeSimulator.cs b/Assets/Scripts/Rope/RopeSimulator.cs new file mode 100644 index 0000000..d7bf275 --- /dev/null +++ b/Assets/Scripts/Rope/RopeSimulator.cs @@ -0,0 +1,129 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityUtils; + +public class RopeSimulator : MonoBehaviour +{ + [SerializeField] + private float gravity = 10; + + [SerializeField] + private float solveIterations = 10; + + [SerializeField] + private float constrainStickMinLength = 0.1f; + + int[] order; + + public Vector2 testPos; + + Rope rope; + + private void Start() + { + rope = new RopeBuilder() + .AddPoint(new Point(testPos, locked: true)) + .AddPoint(new Point(testPos.Add(x:5f))) + .AddPoint(new Point(testPos.Add(x: 10f))) + .AddPoint(new Point(testPos.Add(x: 15f))) + .AddPoint(new Point(testPos.Add(x: 20f))) + .ConnectPoints(0, 1) + .ConnectPoints(1, 2) + .ConnectPoints(2, 3) + .ConnectPoints(3, 4) + .Build(); + CreateOrderArray(); + } + + private void Update() + { + Simulate(); + } + + private void OnDrawGizmos() + { + if (!Application.isPlaying) return; + + foreach (var point in rope.points) + { + Debug.Log($"pos: {point.position}"); + Gizmos.DrawSphere(point.position, 1f); + } + } + + void Simulate() + { + foreach (Point p in rope.points) + { + if (!p.locked) + { + Vector2 positionBeforeUpdate = p.position; + p.position += p.position - p.prevPosition; + p.position += Vector2.down * gravity * Time.deltaTime * Time.deltaTime; + p.prevPosition = positionBeforeUpdate; + } + } + + for (int i = 0; i < solveIterations; i++) + { + for (int s = 0; s < rope.sticks.Count; s++) + { + Stick stick = rope.sticks[order[s]]; + if (stick.dead) + { + continue; + } + + Vector2 stickCentre = (stick.A.position + stick.B.position) / 2; + Vector2 stickDir = (stick.A.position - stick.B.position).normalized; + float length = (stick.A.position - stick.B.position).magnitude; + + if (length > stick.length || length > constrainStickMinLength) + { + if (!stick.A.locked) + { + stick.A.position = stickCentre + stickDir * stick.length / 2; + } + if (!stick.B.locked) + { + stick.B.position = stickCentre - stickDir * stick.length / 2; + } + } + + } + } + } + + void CreateOrderArray() + { + order = new int[rope.sticks.Count]; + for (int i = 0; i < order.Length; i++) + { + order[i] = i; + } + ShuffleArray(order, new System.Random()); + } + + public static T[] ShuffleArray(T[] array, System.Random prng) + { + + int elementsRemainingToShuffle = array.Length; + int randomIndex = 0; + + while (elementsRemainingToShuffle > 1) + { + + // Choose a random element from array + randomIndex = prng.Next(0, elementsRemainingToShuffle); + T chosenElement = array[randomIndex]; + + // Swap the randomly chosen element with the last unshuffled element in the array + elementsRemainingToShuffle--; + array[randomIndex] = array[elementsRemainingToShuffle]; + array[elementsRemainingToShuffle] = chosenElement; + } + + return array; + } +} diff --git a/Assets/Scripts/Rope/RopeSimulator.cs.meta b/Assets/Scripts/Rope/RopeSimulator.cs.meta new file mode 100644 index 0000000..632c74c --- /dev/null +++ b/Assets/Scripts/Rope/RopeSimulator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 27ac133d9e10e544ba603e07122e3359 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Rope/Stick.cs b/Assets/Scripts/Rope/Stick.cs new file mode 100644 index 0000000..078e0fe --- /dev/null +++ b/Assets/Scripts/Rope/Stick.cs @@ -0,0 +1,16 @@ +using UnityEngine; + +[System.Serializable] +public class Stick +{ + public Point A, B; + public float length; + public bool dead; + + public Stick(Point pointA, Point pointB) + { + this.A = pointA; + this.B = pointB; + length = Vector2.Distance(pointA.position, pointB.position); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Rope/Stick.cs.meta b/Assets/Scripts/Rope/Stick.cs.meta new file mode 100644 index 0000000..0ce0dd4 --- /dev/null +++ b/Assets/Scripts/Rope/Stick.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 61cc19e8607c5c243941e77804b59b30 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: