From 7ead561296c2fae219e9f9eac262768307f72636 Mon Sep 17 00:00:00 2001 From: Samir Badi Date: Mon, 30 Oct 2023 22:58:29 -0400 Subject: [PATCH] Adjust player facing direction base on movement --- Assets/Scenes/SampleScene.unity | 305 +++++++++++++++++- Assets/Scripts/GameInputs.cs | 90 ++---- Assets/Scripts/GameInputs.inputactions | 90 ++---- Assets/Scripts/Players/PlayerInputHandler.cs | 6 +- Assets/Scripts/Players/PlayerMain.cs | 16 + .../Scripts/Players/PlayerMain_Animation.cs | 24 ++ .../Players/PlayerMain_Animation.cs.meta | 11 + Assets/Scripts/Players/PlayerMain_Movement.cs | 58 +++- ProjectSettings/TagManager.asset | 2 +- 9 files changed, 478 insertions(+), 124 deletions(-) create mode 100644 Assets/Scripts/Players/PlayerMain_Animation.cs create mode 100644 Assets/Scripts/Players/PlayerMain_Animation.cs.meta diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index d5f5c10..006744f 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -342,6 +342,90 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &899502650 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 899502651} + - component: {fileID: 899502652} + m_Layer: 0 + m_Name: Visual (1) + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &899502651 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 899502650} + serializedVersion: 2 + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0.62, y: 0.253, z: 0} + m_LocalScale: {x: 0.45, y: 0.29, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 1505234463} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!212 &899502652 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 899502650} + 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: 7482667652216324306, guid: 48e93eef0688c4a259cb0eddcd8661f7, type: 3} + m_Color: {r: 0, g: 0.88305783, b: 1, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 1, y: 2} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 --- !u!1 &1094939059 GameObject: m_ObjectHideFlags: 0 @@ -354,6 +438,7 @@ GameObject: - component: {fileID: 1094939062} - component: {fileID: 1094939061} - component: {fileID: 1094939060} + - component: {fileID: 1094939064} m_Layer: 0 m_Name: PlayerMain m_TagString: Untagged @@ -376,8 +461,8 @@ Rigidbody2D: m_Mass: 1 m_LinearDrag: 0 m_AngularDrag: 0.05 - m_GravityScale: 0 - m_Material: {fileID: 0} + m_GravityScale: 15 + m_Material: {fileID: 6200000, guid: b703f35e8d8996a4794fdd66fffd0dc9, type: 2} m_IncludeLayers: serializedVersion: 2 m_Bits: 0 @@ -400,7 +485,14 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: d673274af6c36104abfc3cb26d0d9d80, type: 3} m_Name: m_EditorClassIdentifier: - speed: 4 + visualTransform: {fileID: 1505234463} + speed: 8 + jumpVelocity: 40 + groundLayer: + serializedVersion: 2 + m_Bits: 64 + groundDistance: 0.2 + drawDebugRaycasts: 1 --- !u!114 &1094939062 MonoBehaviour: m_ObjectHideFlags: 0 @@ -429,6 +521,209 @@ Transform: - {fileID: 1505234463} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!61 &1094939064 +BoxCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1094939059} + 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: 0 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: 0, y: 0.75} + m_SpriteTilingProperty: + border: {x: 0, y: 0, z: 0, w: 0} + pivot: {x: 0, y: 0} + oldSize: {x: 0, y: 0} + newSize: {x: 0, y: 0} + adaptiveTilingThreshold: 0 + drawMode: 0 + adaptiveTiling: 0 + m_AutoTiling: 0 + serializedVersion: 2 + m_Size: {x: 1, y: 1.5} + m_EdgeRadius: 0 +--- !u!1 &1463173199 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1463173202} + - component: {fileID: 1463173201} + - component: {fileID: 1463173200} + - component: {fileID: 1463173203} + m_Layer: 6 + m_Name: Ground + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!50 &1463173200 +Rigidbody2D: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1463173199} + m_BodyType: 2 + m_Simulated: 1 + m_UseFullKinematicContacts: 0 + m_UseAutoMass: 0 + m_Mass: 1 + m_LinearDrag: 0 + m_AngularDrag: 0.05 + m_GravityScale: 1 + 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: 0 +--- !u!212 &1463173201 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1463173199} + 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: 7482667652216324306, guid: 311925a002f4447b3a28927169b83ea6, 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 &1463173202 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1463173199} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: -4.5, z: 0} + m_LocalScale: {x: 18, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!61 &1463173203 +BoxCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1463173199} + 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: 0 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: 0, y: 0} + m_SpriteTilingProperty: + border: {x: 0, y: 0, z: 0, w: 0} + pivot: {x: 0.5, y: 0.5} + oldSize: {x: 1, y: 1} + newSize: {x: 1, y: 1} + adaptiveTilingThreshold: 0.5 + drawMode: 0 + adaptiveTiling: 0 + m_AutoTiling: 0 + serializedVersion: 2 + m_Size: {x: 1, y: 1} + m_EdgeRadius: 0 --- !u!1 &1505234462 GameObject: m_ObjectHideFlags: 0 @@ -458,7 +753,8 @@ Transform: m_LocalPosition: {x: 0, y: 0.75, z: 0} m_LocalScale: {x: 1, y: 1.5, z: 1} m_ConstrainProportionsScale: 0 - m_Children: [] + m_Children: + - {fileID: 899502651} m_Father: {fileID: 1094939063} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!212 &1505234464 @@ -520,3 +816,4 @@ SceneRoots: - {fileID: 519420032} - {fileID: 619394802} - {fileID: 1094939063} + - {fileID: 1463173202} diff --git a/Assets/Scripts/GameInputs.cs b/Assets/Scripts/GameInputs.cs index 9d884a2..c48ab6f 100644 --- a/Assets/Scripts/GameInputs.cs +++ b/Assets/Scripts/GameInputs.cs @@ -31,7 +31,7 @@ public partial class @GameInputs: IInputActionCollection2, IDisposable ""name"": ""Movement"", ""type"": ""Value"", ""id"": ""39923aa3-9a9e-4368-8ceb-bebf8ec65530"", - ""expectedControlType"": ""Vector2"", + ""expectedControlType"": ""Axis"", ""processors"": """", ""interactions"": """", ""initialStateCheck"": true @@ -47,61 +47,6 @@ public partial class @GameInputs: IInputActionCollection2, IDisposable } ], ""bindings"": [ - { - ""name"": ""WASD"", - ""id"": ""0abdf8dd-305d-410a-9788-0a0b22384fd7"", - ""path"": ""2DVector(mode=1)"", - ""interactions"": """", - ""processors"": """", - ""groups"": """", - ""action"": ""Movement"", - ""isComposite"": true, - ""isPartOfComposite"": false - }, - { - ""name"": ""up"", - ""id"": ""4a50dfb9-46c0-4d52-ba54-1f4886fb77b8"", - ""path"": ""/w"", - ""interactions"": """", - ""processors"": """", - ""groups"": """", - ""action"": ""Movement"", - ""isComposite"": false, - ""isPartOfComposite"": true - }, - { - ""name"": ""down"", - ""id"": ""a50e92c0-3106-47f9-9499-ff34ca987d4c"", - ""path"": ""/s"", - ""interactions"": """", - ""processors"": """", - ""groups"": """", - ""action"": ""Movement"", - ""isComposite"": false, - ""isPartOfComposite"": true - }, - { - ""name"": ""left"", - ""id"": ""89e61e01-d935-4391-a515-c7f7aac8a744"", - ""path"": ""/a"", - ""interactions"": """", - ""processors"": """", - ""groups"": """", - ""action"": ""Movement"", - ""isComposite"": false, - ""isPartOfComposite"": true - }, - { - ""name"": ""right"", - ""id"": ""3a6ca3be-d76f-4ab1-ae0f-0193d87f36c0"", - ""path"": ""/d"", - ""interactions"": """", - ""processors"": """", - ""groups"": """", - ""action"": ""Movement"", - ""isComposite"": false, - ""isPartOfComposite"": true - }, { ""name"": """", ""id"": ""f79d7b08-89bf-4b67-bba8-5ccaaea5501d"", @@ -112,6 +57,39 @@ public partial class @GameInputs: IInputActionCollection2, IDisposable ""action"": ""Jump"", ""isComposite"": false, ""isPartOfComposite"": false + }, + { + ""name"": ""AD"", + ""id"": ""3ee35364-2c5b-43ec-bcfe-5cceafd1bf45"", + ""path"": ""1DAxis"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Movement"", + ""isComposite"": true, + ""isPartOfComposite"": false + }, + { + ""name"": ""negative"", + ""id"": ""c9f367f7-68bb-47d3-aa78-d9d579a69db0"", + ""path"": ""/a"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Movement"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": ""positive"", + ""id"": ""1b04a4aa-9990-4bcd-97b2-b98061031ba4"", + ""path"": ""/d"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Movement"", + ""isComposite"": false, + ""isPartOfComposite"": true } ] } diff --git a/Assets/Scripts/GameInputs.inputactions b/Assets/Scripts/GameInputs.inputactions index 0be9b38..beb0a56 100644 --- a/Assets/Scripts/GameInputs.inputactions +++ b/Assets/Scripts/GameInputs.inputactions @@ -9,7 +9,7 @@ "name": "Movement", "type": "Value", "id": "39923aa3-9a9e-4368-8ceb-bebf8ec65530", - "expectedControlType": "Vector2", + "expectedControlType": "Axis", "processors": "", "interactions": "", "initialStateCheck": true @@ -25,61 +25,6 @@ } ], "bindings": [ - { - "name": "WASD", - "id": "0abdf8dd-305d-410a-9788-0a0b22384fd7", - "path": "2DVector(mode=1)", - "interactions": "", - "processors": "", - "groups": "", - "action": "Movement", - "isComposite": true, - "isPartOfComposite": false - }, - { - "name": "up", - "id": "4a50dfb9-46c0-4d52-ba54-1f4886fb77b8", - "path": "/w", - "interactions": "", - "processors": "", - "groups": "", - "action": "Movement", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "down", - "id": "a50e92c0-3106-47f9-9499-ff34ca987d4c", - "path": "/s", - "interactions": "", - "processors": "", - "groups": "", - "action": "Movement", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "left", - "id": "89e61e01-d935-4391-a515-c7f7aac8a744", - "path": "/a", - "interactions": "", - "processors": "", - "groups": "", - "action": "Movement", - "isComposite": false, - "isPartOfComposite": true - }, - { - "name": "right", - "id": "3a6ca3be-d76f-4ab1-ae0f-0193d87f36c0", - "path": "/d", - "interactions": "", - "processors": "", - "groups": "", - "action": "Movement", - "isComposite": false, - "isPartOfComposite": true - }, { "name": "", "id": "f79d7b08-89bf-4b67-bba8-5ccaaea5501d", @@ -90,6 +35,39 @@ "action": "Jump", "isComposite": false, "isPartOfComposite": false + }, + { + "name": "AD", + "id": "3ee35364-2c5b-43ec-bcfe-5cceafd1bf45", + "path": "1DAxis", + "interactions": "", + "processors": "", + "groups": "", + "action": "Movement", + "isComposite": true, + "isPartOfComposite": false + }, + { + "name": "negative", + "id": "c9f367f7-68bb-47d3-aa78-d9d579a69db0", + "path": "/a", + "interactions": "", + "processors": "", + "groups": "", + "action": "Movement", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "positive", + "id": "1b04a4aa-9990-4bcd-97b2-b98061031ba4", + "path": "/d", + "interactions": "", + "processors": "", + "groups": "", + "action": "Movement", + "isComposite": false, + "isPartOfComposite": true } ] } diff --git a/Assets/Scripts/Players/PlayerInputHandler.cs b/Assets/Scripts/Players/PlayerInputHandler.cs index b1567a5..9d838d0 100644 --- a/Assets/Scripts/Players/PlayerInputHandler.cs +++ b/Assets/Scripts/Players/PlayerInputHandler.cs @@ -9,7 +9,7 @@ using UnityEngine.PlayerLoop; public class PlayerInputHandler : MonoBehaviour { public event EventHandler OnJumpTrigger; - public Vector2 MoveDir { get; private set; } + public float MoveAxis { get; private set; } private GameInputs _input; private bool _isReadyToClear; @@ -48,7 +48,7 @@ public class PlayerInputHandler : MonoBehaviour private void ProcessInput() { - MoveDir = _input.Player.Movement.ReadValue(); + MoveAxis = _input.Player.Movement.ReadValue(); } private void ClearInput() @@ -56,7 +56,7 @@ public class PlayerInputHandler : MonoBehaviour if (!_isReadyToClear) return; - MoveDir = Vector2.zero; + MoveAxis = 0f; _isReadyToClear = false; } diff --git a/Assets/Scripts/Players/PlayerMain.cs b/Assets/Scripts/Players/PlayerMain.cs index 31ad1b9..0e063ad 100644 --- a/Assets/Scripts/Players/PlayerMain.cs +++ b/Assets/Scripts/Players/PlayerMain.cs @@ -9,6 +9,7 @@ public partial class PlayerMain : MonoBehaviour private PlayerInputHandler _input; private Rigidbody2D _rigidbody2D; + private BoxCollider2D _collider2D; private void Awake() @@ -24,10 +25,25 @@ public partial class PlayerMain : MonoBehaviour _input = GetComponent(); _rigidbody2D = GetComponent(); + _collider2D = GetComponent(); + } + + private void Start() + { + _input.OnJumpTrigger += Input_OnJumpTrigger; + + _footOffset = _collider2D.size.x / 2f; + _facingDir = visualTransform.localScale.x >= 0f ? 1f : -1f; + } + + private void Update() + { + PhysicsCheck(); } private void FixedUpdate() { MovementHandler(); + FacingDir(); } } diff --git a/Assets/Scripts/Players/PlayerMain_Animation.cs b/Assets/Scripts/Players/PlayerMain_Animation.cs new file mode 100644 index 0000000..0f44bac --- /dev/null +++ b/Assets/Scripts/Players/PlayerMain_Animation.cs @@ -0,0 +1,24 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public partial class PlayerMain +{ + [Header("Animation")] + [SerializeField] private Transform visualTransform; + + private const float THRESHOLD = 0.01f; + + private float _facingDir; + + private void FacingDir() + { + if (_facingDir * _input.MoveAxis > -THRESHOLD) + return; + + Vector3 scale = visualTransform.localScale; + _facingDir *= -1f; + scale.x *= -1f; + visualTransform.localScale = scale; + } +} \ No newline at end of file diff --git a/Assets/Scripts/Players/PlayerMain_Animation.cs.meta b/Assets/Scripts/Players/PlayerMain_Animation.cs.meta new file mode 100644 index 0000000..b212a9a --- /dev/null +++ b/Assets/Scripts/Players/PlayerMain_Animation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ef933310dc90f6145a0fecb819938dda +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Players/PlayerMain_Movement.cs b/Assets/Scripts/Players/PlayerMain_Movement.cs index de6013e..72281ea 100644 --- a/Assets/Scripts/Players/PlayerMain_Movement.cs +++ b/Assets/Scripts/Players/PlayerMain_Movement.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -5,14 +6,63 @@ using UnityEngine; public partial class PlayerMain { [Header("Movement")] - [SerializeField] private float speed = 4f; + [SerializeField] private float speed = 8f; + [SerializeField] private float jumpVelocity = 100f; + + [Header("Ground")] + [SerializeField] private LayerMask groundLayer; + [SerializeField] private float groundDistance = 0.2f; + [SerializeField] private bool drawDebugRaycasts; + private bool _isGrounded; + private float _footOffset; + + + private void Input_OnJumpTrigger(object sender, EventArgs e) + { + HandleJump(); + } private void MovementHandler() { - Vector2 move = _input.MoveDir; - move.y = 0f; + Vector2 move = new Vector2(_input.MoveAxis * speed, _rigidbody2D.velocity.y); - _rigidbody2D.velocity = move * speed; + _rigidbody2D.velocity = move; + } + + private void HandleJump() + { + if (_isGrounded) + _rigidbody2D.velocity = Vector2.up * jumpVelocity; + } + + void PhysicsCheck() + { + _isGrounded = false; + + RaycastHit2D leftCheck = Raycast(new Vector2(-_footOffset, 0f), Vector2.down, groundDistance); + RaycastHit2D rightCheck = Raycast(new Vector2(_footOffset, 0f), Vector2.down, groundDistance); + + if (leftCheck || rightCheck) + _isGrounded = true; + } + + RaycastHit2D Raycast(Vector2 offset, Vector2 rayDirection, float length) + { + return Raycast(offset, rayDirection, length, groundLayer); + } + + RaycastHit2D Raycast(Vector2 offset, Vector2 rayDirection, float length, LayerMask mask) + { + Vector2 pos = transform.position; + RaycastHit2D hit = Physics2D.Raycast(pos + offset, rayDirection, length, mask); + + if (drawDebugRaycasts) + { + Color color = hit ? Color.red : Color.green; + Debug.DrawRay(pos + offset, rayDirection * length, color); + } + + return hit; } } \ No newline at end of file diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index 1c92a78..588a62e 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -11,7 +11,7 @@ TagManager: - - Water - UI - - + - Ground - - -