Improved jumping and moving

This commit is contained in:
OverflowNarhoym 2022-02-22 21:49:06 +01:00
parent cb09cfa4a7
commit 9d0f9e620c
3 changed files with 67 additions and 45 deletions

View File

@ -123,7 +123,7 @@ Transform:
m_GameObject: {fileID: 7008207192594766305}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -4.55, y: 0.41, z: 0}
m_LocalScale: {x: 1, y: 2, z: 1}
m_LocalScale: {x: 1, y: 1.5, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 7008207193600475140}
@ -197,7 +197,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 1e9f5de2199c4784abce74247804ce87, type: 3}
m_Name:
m_EditorClassIdentifier:
speed: 0
--- !u!50 &7008207192594766309
Rigidbody2D:
serializedVersion: 4

View File

@ -9,17 +9,20 @@ namespace Player
{
public class PlayerController : MonoBehaviour, IMovement, PlayerInput.IPlayerControlActions
{
private const float DefaultJumpForce = 25.0f;
private const float DefaultJumpForce = 26.0f;
private const float DefaultMass = 80.0f;
private const float MaxSpeed = 500.0f;
private const float DefaultAirSpeed = 300.0f;
private const float GravityScale = 5.0f;
private const float MaxSpeed = 700.0f;
private const float MaxAirSpeed = 500.0f;
private const float GravityScale = 6.0f;
private const float AccelerationScale = 40.0f;
private const float DecelerationScale = 60.0f;
private const float RespawnLimit = -60.0f;
private const float FallMultiplier = 5.0f;
private const float LowJumpMultiplier = 20.0f;
//Took from tutorial : https://www.youtube.com/watch?v=7KiK0Aqtmzc
private const float FallMultiplier = 3.0f;
private const float LowJumpMultiplier = 40.0f;
//End of credits
private PlayerInput _controls;
private Rigidbody2D _playerRigidbody2D;
@ -27,6 +30,8 @@ namespace Player
private CollisionChecker _playerGroundTrigger;
private CollisionChecker _playerInteractableTrigger;
private CollisionChecker _playerWallTriggerLeft;
private CollisionChecker _playerWallTriggerRight;
private Orientation _orientation;
@ -34,7 +39,7 @@ namespace Player
private bool _jumpKeyPressed;
private float _xAxisValue;
public float speed;
private float _speed;
private bool _isOnAir;
private bool _canJump;
@ -46,6 +51,8 @@ namespace Player
_playerSpriteRenderer = GetComponent<SpriteRenderer>();
_playerGroundTrigger = GameObject.Find("PlayerGroundTrigger").GetComponent<CollisionChecker>();
_playerInteractableTrigger = GameObject.Find("PlayerInteractableTrigger").GetComponent<CollisionChecker>();
_playerWallTriggerLeft = GameObject.Find("PlayerWallTriggerLeft").GetComponent<CollisionChecker>();
_playerWallTriggerRight = GameObject.Find("PlayerWallTriggerRight").GetComponent<CollisionChecker>();
}
private void Start()
@ -81,6 +88,7 @@ namespace Player
private void FixedUpdate()
{
//Took from tutorial : https://www.youtube.com/watch?v=7KiK0Aqtmzc
switch (_playerRigidbody2D.velocity.y)
{
case < 0:
@ -92,13 +100,14 @@ namespace Player
Vector2.up * (Physics2D.gravity.y * (LowJumpMultiplier - 1) * Time.fixedDeltaTime);
break;
}
//End of credits
if (_moveKeyPressed)
Move(_xAxisValue);
else
_playerRigidbody2D.velocity = new Vector2(0.0f, _playerRigidbody2D.velocity.y);
if (_jumpKeyPressed && _canJump && _playerGroundTrigger.IsCollided)
if (_jumpKeyPressed && _playerGroundTrigger.IsCollided && _canJump)
Jump();
if (_afterMoving)
@ -137,15 +146,26 @@ namespace Player
public float BaseSpeed
{
get => speed;
get => _speed;
set
{
speed = speed switch
switch (_isOnAir)
{
> MaxSpeed => MaxSpeed,
< 0 => 0,
_ => value
};
case false when value > MaxSpeed:
_speed = MaxSpeed;
break;
case true when value > MaxAirSpeed:
_speed = MaxAirSpeed;
break;
default:
{
if (value < 0)
_speed = 0;
else
_speed = value;
break;
}
}
}
}
@ -156,17 +176,19 @@ namespace Player
case < 0:
_playerSpriteRenderer.flipX = true;
_orientation = Orientation.Left;
//prevent player from sticking to wall while moving to its direction in the air
if (!_playerGroundTrigger.IsCollided && _playerWallTriggerLeft.IsCollided)
return;
break;
case > 0:
_playerSpriteRenderer.flipX = false;
_orientation = Orientation.Right;
break;
default:
_playerSpriteRenderer.flipX = _playerSpriteRenderer.flipX;
//prevent player from sticking to wall while moving to its direction in the air
if (!_playerGroundTrigger.IsCollided && _playerWallTriggerRight.IsCollided)
return;
break;
}
if (!_isOnAir)
BaseSpeed += AccelerationScale;
_playerRigidbody2D.velocity = new Vector2(BaseSpeed * _xAxisValue * Time.fixedDeltaTime,
_playerRigidbody2D.velocity.y);
@ -183,7 +205,7 @@ namespace Player
private void Jump()
{
BaseSpeed = DefaultAirSpeed;
BaseSpeed = MaxAirSpeed;
_playerRigidbody2D.velocity = Vector2.up.normalized * DefaultJumpForce;
_canJump = false;
}
@ -220,6 +242,7 @@ namespace Player
_canJump = true;
break;
case false:
if (_playerGroundTrigger.IsCollided)
_jumpKeyPressed = true;
break;
}

View File

@ -25,7 +25,7 @@ MonoBehaviour:
m_MinSize: {x: 300, y: 200}
m_MaxSize: {x: 24288, y: 16192}
vertical: 0
controlID: 509
controlID: 6655
--- !u!114 &2
MonoBehaviour:
m_ObjectHideFlags: 52
@ -142,7 +142,7 @@ MonoBehaviour:
m_MinSize: {x: 100, y: 200}
m_MaxSize: {x: 8096, y: 16192}
vertical: 1
controlID: 465
controlID: 6632
--- !u!114 &4
MonoBehaviour:
m_ObjectHideFlags: 52
@ -162,8 +162,8 @@ MonoBehaviour:
y: 0
width: 303.2
height: 381.6
m_MinSize: {x: 200, y: 200}
m_MaxSize: {x: 4000, y: 4000}
m_MinSize: {x: 201, y: 221}
m_MaxSize: {x: 4001, y: 4021}
m_ActualView: {fileID: 5}
m_Panes:
- {fileID: 5}
@ -202,7 +202,7 @@ MonoBehaviour:
scrollPos: {x: 0, y: 0}
m_SelectedIDs:
m_LastClickedID: 0
m_ExpandedIDs: 0afbffff0cfbfffff6620000
m_ExpandedIDs: ecfaffff0afbffff0cfbfffff6620000
m_RenameOverlay:
m_UserAcceptedRename: 0
m_Name:
@ -245,8 +245,8 @@ MonoBehaviour:
y: 381.6
width: 303.2
height: 349.19998
m_MinSize: {x: 230, y: 250}
m_MaxSize: {x: 10000, y: 10000}
m_MinSize: {x: 231, y: 271}
m_MaxSize: {x: 10001, y: 10021}
m_ActualView: {fileID: 7}
m_Panes:
- {fileID: 7}
@ -334,7 +334,7 @@ MonoBehaviour:
m_ResourceFile:
m_AssetTreeState:
scrollPos: {x: 0, y: 0}
m_SelectedIDs: 06fbffff
m_SelectedIDs: fc620000
m_LastClickedID: 0
m_ExpandedIDs: ffffffff000000004c630000a6630000a8630000aa630000ac630000ae630000b0630000b2630000b4630000b6630000b8630000ba630000bc630000be630000c2630000c4630000
m_RenameOverlay:
@ -361,8 +361,8 @@ MonoBehaviour:
m_Icon: {fileID: 0}
m_ResourceFile:
m_ListAreaState:
m_SelectedInstanceIDs: 06fbffff
m_LastClickedInstanceID: -1274
m_SelectedInstanceIDs: fc620000
m_LastClickedInstanceID: 25340
m_HadKeyboardFocusLastEvent: 0
m_ExpandedInstanceIDs: c6230000d03c0000
m_RenameOverlay:
@ -444,7 +444,7 @@ MonoBehaviour:
m_MinSize: {x: 100, y: 200}
m_MaxSize: {x: 8096, y: 16192}
vertical: 1
controlID: 510
controlID: 6656
--- !u!114 &10
MonoBehaviour:
m_ObjectHideFlags: 52
@ -603,7 +603,7 @@ MonoBehaviour:
containerId: overlay-container--right
floating: 0
collapsed: 0
displayed: 1
displayed: 0
snapOffset: {x: 0, y: 0}
snapOffsetDelta: {x: 0, y: 0}
snapCorner: 0
@ -719,9 +719,9 @@ MonoBehaviour:
m_PlayAudio: 0
m_AudioPlay: 0
m_Position:
m_Target: {x: -17.629921, y: 0.48958027, z: -0.22409801}
m_Target: {x: -25.330248, y: -2.0593255, z: -0.20706767}
speed: 2
m_Value: {x: -17.629921, y: 0.48958027, z: -0.22409801}
m_Value: {x: -25.330248, y: -2.0593255, z: -0.20706767}
m_RenderMode: 0
m_CameraMode:
drawMode: 0
@ -772,9 +772,9 @@ MonoBehaviour:
speed: 2
m_Value: {x: 0, y: 0, z: 0, w: 1}
m_Size:
m_Target: 21.067785
m_Target: 14.378537
speed: 2
m_Value: 21.067785
m_Value: 14.378537
m_Ortho:
m_Target: 1
speed: 2
@ -853,8 +853,8 @@ MonoBehaviour:
y: 399.2
width: 879.99994
height: 331.59998
m_MinSize: {x: 200, y: 200}
m_MaxSize: {x: 4000, y: 4000}
m_MinSize: {x: 202, y: 221}
m_MaxSize: {x: 4002, y: 4021}
m_ActualView: {fileID: 2}
m_Panes:
- {fileID: 2}
@ -879,8 +879,8 @@ MonoBehaviour:
y: 0
width: 352.80005
height: 730.8
m_MinSize: {x: 275, y: 50}
m_MaxSize: {x: 4000, y: 4000}
m_MinSize: {x: 276, y: 71}
m_MaxSize: {x: 4001, y: 4021}
m_ActualView: {fileID: 15}
m_Panes:
- {fileID: 15}
@ -920,8 +920,8 @@ MonoBehaviour:
m_CachedPref: 160
m_ControlHash: -371814159
m_PrefName: Preview_InspectorPreview
m_LastInspectedObjectInstanceID: -1274
m_LastVerticalScrollValue: 334.40002
m_LastInspectedObjectInstanceID: 25340
m_LastVerticalScrollValue: 0
m_GlobalObjectId:
m_InspectorMode: 0
m_LockTracker: