Adjust player facing direction base on movement

This commit is contained in:
Samir Badi 2023-10-30 22:58:29 -04:00
parent 1d79ba4c6c
commit 7ead561296
9 changed files with 478 additions and 124 deletions

View File

@ -342,6 +342,90 @@ Transform:
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 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 --- !u!1 &1094939059
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -354,6 +438,7 @@ GameObject:
- component: {fileID: 1094939062} - component: {fileID: 1094939062}
- component: {fileID: 1094939061} - component: {fileID: 1094939061}
- component: {fileID: 1094939060} - component: {fileID: 1094939060}
- component: {fileID: 1094939064}
m_Layer: 0 m_Layer: 0
m_Name: PlayerMain m_Name: PlayerMain
m_TagString: Untagged m_TagString: Untagged
@ -376,8 +461,8 @@ Rigidbody2D:
m_Mass: 1 m_Mass: 1
m_LinearDrag: 0 m_LinearDrag: 0
m_AngularDrag: 0.05 m_AngularDrag: 0.05
m_GravityScale: 0 m_GravityScale: 15
m_Material: {fileID: 0} m_Material: {fileID: 6200000, guid: b703f35e8d8996a4794fdd66fffd0dc9, type: 2}
m_IncludeLayers: m_IncludeLayers:
serializedVersion: 2 serializedVersion: 2
m_Bits: 0 m_Bits: 0
@ -400,7 +485,14 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d673274af6c36104abfc3cb26d0d9d80, type: 3} m_Script: {fileID: 11500000, guid: d673274af6c36104abfc3cb26d0d9d80, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: 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 --- !u!114 &1094939062
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -429,6 +521,209 @@ Transform:
- {fileID: 1505234463} - {fileID: 1505234463}
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 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 --- !u!1 &1505234462
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -458,7 +753,8 @@ Transform:
m_LocalPosition: {x: 0, y: 0.75, z: 0} m_LocalPosition: {x: 0, y: 0.75, z: 0}
m_LocalScale: {x: 1, y: 1.5, z: 1} m_LocalScale: {x: 1, y: 1.5, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children:
- {fileID: 899502651}
m_Father: {fileID: 1094939063} m_Father: {fileID: 1094939063}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!212 &1505234464 --- !u!212 &1505234464
@ -520,3 +816,4 @@ SceneRoots:
- {fileID: 519420032} - {fileID: 519420032}
- {fileID: 619394802} - {fileID: 619394802}
- {fileID: 1094939063} - {fileID: 1094939063}
- {fileID: 1463173202}

View File

@ -31,7 +31,7 @@ public partial class @GameInputs: IInputActionCollection2, IDisposable
""name"": ""Movement"", ""name"": ""Movement"",
""type"": ""Value"", ""type"": ""Value"",
""id"": ""39923aa3-9a9e-4368-8ceb-bebf8ec65530"", ""id"": ""39923aa3-9a9e-4368-8ceb-bebf8ec65530"",
""expectedControlType"": ""Vector2"", ""expectedControlType"": ""Axis"",
""processors"": """", ""processors"": """",
""interactions"": """", ""interactions"": """",
""initialStateCheck"": true ""initialStateCheck"": true
@ -47,61 +47,6 @@ public partial class @GameInputs: IInputActionCollection2, IDisposable
} }
], ],
""bindings"": [ ""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"": ""<Keyboard>/w"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Movement"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""down"",
""id"": ""a50e92c0-3106-47f9-9499-ff34ca987d4c"",
""path"": ""<Keyboard>/s"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Movement"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""left"",
""id"": ""89e61e01-d935-4391-a515-c7f7aac8a744"",
""path"": ""<Keyboard>/a"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Movement"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""right"",
""id"": ""3a6ca3be-d76f-4ab1-ae0f-0193d87f36c0"",
""path"": ""<Keyboard>/d"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Movement"",
""isComposite"": false,
""isPartOfComposite"": true
},
{ {
""name"": """", ""name"": """",
""id"": ""f79d7b08-89bf-4b67-bba8-5ccaaea5501d"", ""id"": ""f79d7b08-89bf-4b67-bba8-5ccaaea5501d"",
@ -112,6 +57,39 @@ public partial class @GameInputs: IInputActionCollection2, IDisposable
""action"": ""Jump"", ""action"": ""Jump"",
""isComposite"": false, ""isComposite"": false,
""isPartOfComposite"": 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"": ""<Keyboard>/a"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Movement"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""positive"",
""id"": ""1b04a4aa-9990-4bcd-97b2-b98061031ba4"",
""path"": ""<Keyboard>/d"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Movement"",
""isComposite"": false,
""isPartOfComposite"": true
} }
] ]
} }

View File

@ -9,7 +9,7 @@
"name": "Movement", "name": "Movement",
"type": "Value", "type": "Value",
"id": "39923aa3-9a9e-4368-8ceb-bebf8ec65530", "id": "39923aa3-9a9e-4368-8ceb-bebf8ec65530",
"expectedControlType": "Vector2", "expectedControlType": "Axis",
"processors": "", "processors": "",
"interactions": "", "interactions": "",
"initialStateCheck": true "initialStateCheck": true
@ -25,61 +25,6 @@
} }
], ],
"bindings": [ "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": "<Keyboard>/w",
"interactions": "",
"processors": "",
"groups": "",
"action": "Movement",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "down",
"id": "a50e92c0-3106-47f9-9499-ff34ca987d4c",
"path": "<Keyboard>/s",
"interactions": "",
"processors": "",
"groups": "",
"action": "Movement",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "left",
"id": "89e61e01-d935-4391-a515-c7f7aac8a744",
"path": "<Keyboard>/a",
"interactions": "",
"processors": "",
"groups": "",
"action": "Movement",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "right",
"id": "3a6ca3be-d76f-4ab1-ae0f-0193d87f36c0",
"path": "<Keyboard>/d",
"interactions": "",
"processors": "",
"groups": "",
"action": "Movement",
"isComposite": false,
"isPartOfComposite": true
},
{ {
"name": "", "name": "",
"id": "f79d7b08-89bf-4b67-bba8-5ccaaea5501d", "id": "f79d7b08-89bf-4b67-bba8-5ccaaea5501d",
@ -90,6 +35,39 @@
"action": "Jump", "action": "Jump",
"isComposite": false, "isComposite": false,
"isPartOfComposite": 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": "<Keyboard>/a",
"interactions": "",
"processors": "",
"groups": "",
"action": "Movement",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "positive",
"id": "1b04a4aa-9990-4bcd-97b2-b98061031ba4",
"path": "<Keyboard>/d",
"interactions": "",
"processors": "",
"groups": "",
"action": "Movement",
"isComposite": false,
"isPartOfComposite": true
} }
] ]
} }

View File

@ -9,7 +9,7 @@ using UnityEngine.PlayerLoop;
public class PlayerInputHandler : MonoBehaviour public class PlayerInputHandler : MonoBehaviour
{ {
public event EventHandler OnJumpTrigger; public event EventHandler OnJumpTrigger;
public Vector2 MoveDir { get; private set; } public float MoveAxis { get; private set; }
private GameInputs _input; private GameInputs _input;
private bool _isReadyToClear; private bool _isReadyToClear;
@ -48,7 +48,7 @@ public class PlayerInputHandler : MonoBehaviour
private void ProcessInput() private void ProcessInput()
{ {
MoveDir = _input.Player.Movement.ReadValue<Vector2>(); MoveAxis = _input.Player.Movement.ReadValue<float>();
} }
private void ClearInput() private void ClearInput()
@ -56,7 +56,7 @@ public class PlayerInputHandler : MonoBehaviour
if (!_isReadyToClear) if (!_isReadyToClear)
return; return;
MoveDir = Vector2.zero; MoveAxis = 0f;
_isReadyToClear = false; _isReadyToClear = false;
} }

View File

@ -9,6 +9,7 @@ public partial class PlayerMain : MonoBehaviour
private PlayerInputHandler _input; private PlayerInputHandler _input;
private Rigidbody2D _rigidbody2D; private Rigidbody2D _rigidbody2D;
private BoxCollider2D _collider2D;
private void Awake() private void Awake()
@ -24,10 +25,25 @@ public partial class PlayerMain : MonoBehaviour
_input = GetComponent<PlayerInputHandler>(); _input = GetComponent<PlayerInputHandler>();
_rigidbody2D = GetComponent<Rigidbody2D>(); _rigidbody2D = GetComponent<Rigidbody2D>();
_collider2D = GetComponent<BoxCollider2D>();
}
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() private void FixedUpdate()
{ {
MovementHandler(); MovementHandler();
FacingDir();
} }
} }

View File

@ -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;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ef933310dc90f6145a0fecb819938dda
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,3 +1,4 @@
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
@ -5,14 +6,63 @@ using UnityEngine;
public partial class PlayerMain public partial class PlayerMain
{ {
[Header("Movement")] [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() private void MovementHandler()
{ {
Vector2 move = _input.MoveDir; Vector2 move = new Vector2(_input.MoveAxis * speed, _rigidbody2D.velocity.y);
move.y = 0f;
_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;
} }
} }

View File

@ -11,7 +11,7 @@ TagManager:
- -
- Water - Water
- UI - UI
- - Ground
- -
- -
- -