using System.ComponentModel.Design;
|
using UnityEngine;
|
using UnityEngine.InputSystem;
|
|
[RequireComponent(typeof(Rigidbody2D))]
|
public class PlayerController : MonoBehaviour
|
{
|
public float moveSpeed = 5f;
|
public float maxFallSpeed = -20f;
|
Vector2 moveInput;
|
|
public bool IsMoving { get; private set; }
|
|
Rigidbody2D rb;
|
|
private void Awake()
|
{
|
rb = GetComponent<Rigidbody2D>();
|
}
|
|
// Start is called before the first frame update
|
void Start()
|
{
|
|
}
|
|
// Update is called once per frame
|
void Update()
|
{
|
|
}
|
|
private void FixedUpdate()
|
{
|
if (moveInput.y == 0)
|
{
|
if (rb.velocity.y <= maxFallSpeed)
|
{
|
// max fall speed, dont accelerate more
|
rb.velocity = new Vector2(moveInput.x * moveSpeed, maxFallSpeed);
|
}
|
else
|
{
|
// normal fall
|
rb.velocity = new Vector2(moveInput.x * moveSpeed, rb.velocity.y);
|
}
|
}
|
else
|
{
|
if (rb.velocity.y < 0 && moveInput.y > 0)
|
{
|
// falling but moving upwards
|
rb.velocity = new Vector2(moveInput.x * moveSpeed, (moveInput.y * moveSpeed) + rb.velocity.y);
|
}
|
else
|
{
|
// moving upwards no falling
|
rb.velocity = new Vector2(moveInput.x * moveSpeed, (moveInput.y * moveSpeed));
|
}
|
}
|
}
|
|
public void OnMove(InputAction.CallbackContext context)
|
{
|
moveInput = context.ReadValue<Vector2>();
|
|
IsMoving = moveInput != Vector2.zero;
|
|
}
|
}
|