Added movement acceleration
This commit is contained in:
@@ -2,18 +2,21 @@ using System;
|
||||
using Movement;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.SceneManagement;
|
||||
using PlayerInput = Input.PlayerInput;
|
||||
|
||||
namespace Player
|
||||
{
|
||||
public class PlayerController : MonoBehaviour, IMovement, PlayerInput.IPlayerControlActions
|
||||
{
|
||||
private const float DefaultJumpForce = 10.0f;
|
||||
private const float DefaultMass = 40.0f;
|
||||
private const float DefaultSpeed = 500.0f;
|
||||
private const float DefaultJumpForce = 25.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 FallMultiplier = 3f;
|
||||
private const float LowJumpMultiplier = 3f;
|
||||
private const float FallMultiplier = 4.0f;
|
||||
private const float LowJumpMultiplier = 10.0f;
|
||||
|
||||
private PlayerInput _controls;
|
||||
private Rigidbody2D _playerRigidbody2D;
|
||||
@@ -25,9 +28,11 @@ namespace Player
|
||||
private bool _moveKeyPressed;
|
||||
private bool _jumpKeyPressed;
|
||||
|
||||
private bool _isOnAir;
|
||||
|
||||
private float _xAxisValue;
|
||||
public float speed;
|
||||
|
||||
private bool _isOnAir;
|
||||
private bool _canJump;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -40,7 +45,9 @@ namespace Player
|
||||
private void Start()
|
||||
{
|
||||
_playerRigidbody2D.mass = DefaultMass;
|
||||
BaseSpeed = DefaultSpeed;
|
||||
_playerRigidbody2D.gravityScale = GravityScale;
|
||||
_canJump = true;
|
||||
BaseSpeed = 0.0f;
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
@@ -61,31 +68,41 @@ namespace Player
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
switch (_playerRigidbody2D.velocity.y)
|
||||
/*switch (_playerRigidbody2D.velocity.y)
|
||||
{
|
||||
case < 0:
|
||||
_playerRigidbody2D.velocity +=
|
||||
Vector2.up * (Physics2D.gravity.y * (FallMultiplier - 1) * Time.fixedDeltaTime);
|
||||
break;
|
||||
/*case > 0 and < 3:
|
||||
print("We will go down" + i);
|
||||
++i;
|
||||
break;
|
||||
case > 0 when !_jumpKeyPressed:
|
||||
_playerRigidbody2D.velocity +=
|
||||
Vector2.up * (Physics2D.gravity.y * (LowJumpMultiplier - 1) * Time.fixedDeltaTime);
|
||||
break;
|
||||
}
|
||||
}*/
|
||||
|
||||
if (_moveKeyPressed)
|
||||
Move(_xAxisValue);
|
||||
else
|
||||
_playerRigidbody2D.velocity = new Vector2(0.0f, _playerRigidbody2D.velocity.y);
|
||||
|
||||
//TODO prevent jumping while keep pressing the button
|
||||
if (_jumpKeyPressed && _playerGroundTrigger.IsCollided)
|
||||
if (_jumpKeyPressed && _canJump && _playerGroundTrigger.IsCollided)
|
||||
Jump();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
//TODO calculate is on air based on wall and ground triggers
|
||||
_isOnAir = !_playerGroundTrigger.IsCollided;
|
||||
RespawnCheck();
|
||||
}
|
||||
|
||||
private void RespawnCheck()
|
||||
{
|
||||
if (gameObject.transform.position.y < -100)
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
||||
}
|
||||
|
||||
// PAUSE METHODS
|
||||
@@ -106,25 +123,48 @@ namespace Player
|
||||
|
||||
// MOVE METHODS
|
||||
|
||||
public float BaseSpeed { get; set; }
|
||||
public float BaseSpeed
|
||||
{
|
||||
get => speed;
|
||||
set
|
||||
{
|
||||
speed = value;
|
||||
if (speed > MaxSpeed)
|
||||
speed = MaxSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
public void Move(float value)
|
||||
{
|
||||
_playerSpriteRenderer.flipX = _xAxisValue switch
|
||||
switch (_xAxisValue)
|
||||
{
|
||||
< 0 => true,
|
||||
> 0 => false,
|
||||
_ => _playerSpriteRenderer.flipX
|
||||
};
|
||||
case < 0:
|
||||
_playerSpriteRenderer.flipX = true;
|
||||
break;
|
||||
case > 0:
|
||||
_playerSpriteRenderer.flipX = false;
|
||||
break;
|
||||
default:
|
||||
_playerSpriteRenderer.flipX = _playerSpriteRenderer.flipX;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!_isOnAir)
|
||||
BaseSpeed += 40;
|
||||
_playerRigidbody2D.velocity = new Vector2(BaseSpeed * _xAxisValue * Time.fixedDeltaTime,
|
||||
_playerRigidbody2D.velocity.y);
|
||||
}
|
||||
|
||||
private void DecelerationAfterMoving()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private void Jump()
|
||||
{
|
||||
//_playerRigidbody2D.velocity = new Vector2(_playerRigidbody2D.velocity.x, 0);
|
||||
//_playerRigidbody2D.AddForce(Vector2.up.normalized * DefaultJumpForce * DefaultMass, ForceMode2D.Impulse);
|
||||
_playerRigidbody2D.velocity = Vector2.up * DefaultJumpForce;
|
||||
BaseSpeed = DefaultAirSpeed;
|
||||
_playerRigidbody2D.velocity = Vector2.up.normalized * DefaultJumpForce;
|
||||
_canJump = false;
|
||||
}
|
||||
|
||||
private void Climb()
|
||||
@@ -140,6 +180,7 @@ namespace Player
|
||||
{
|
||||
case true:
|
||||
_moveKeyPressed = false;
|
||||
BaseSpeed = 0;
|
||||
break;
|
||||
case false:
|
||||
_moveKeyPressed = true;
|
||||
@@ -154,6 +195,7 @@ namespace Player
|
||||
{
|
||||
case true:
|
||||
_jumpKeyPressed = false;
|
||||
_canJump = true;
|
||||
break;
|
||||
case false:
|
||||
_jumpKeyPressed = true;
|
||||
|
Reference in New Issue
Block a user