Improved jumping and moving
This commit is contained in:
@@ -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
|
||||
|
@@ -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,18 +176,20 @@ 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;
|
||||
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,7 +242,8 @@ namespace Player
|
||||
_canJump = true;
|
||||
break;
|
||||
case false:
|
||||
_jumpKeyPressed = true;
|
||||
if (_playerGroundTrigger.IsCollided)
|
||||
_jumpKeyPressed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user