ofb/Assets/Scripts/PlayerController.cs

124 lines
3.6 KiB
C#
Raw Normal View History

2023-04-09 21:59:32 -07:00
using UnityEngine;
using UnityEngine.InputSystem;
2023-04-09 21:59:32 -07:00
2023-04-17 00:01:49 -07:00
public class PlayerMovement : MonoBehaviour
2023-04-09 21:59:32 -07:00
{
2023-04-17 00:01:49 -07:00
private Rigidbody2D rb;
2023-04-17 00:01:49 -07:00
public float maxRunSpeed;
public float runAcceleration;
public float snappiness = 1;
public float jumpSpeed;
[Range(0,1)] public float airSpeedMultiplier;
2023-04-17 00:01:49 -07:00
private bool onGround = false;
private float forward = 1;
2023-04-17 00:01:49 -07:00
float hangTimeThreshold = 0.1f;
float hangTimeAccel = 0;
float hangTimeSpeed = 0;
private Vector2 movement = Vector2.zero;
public LayerMask groundLayer;
public Vector2 boxSize;
public float maxDistanceFromGround;
[Header("State Control:")]
[SerializeField] private StateController stateController;
2023-04-17 16:47:12 -07:00
PlayerBehavior playerBehavior;
2023-04-17 00:01:49 -07:00
void OnValidate()
2023-04-09 21:59:32 -07:00
{
2023-04-17 00:01:49 -07:00
this.runAcceleration = Mathf.Clamp(runAcceleration, 0.1f, this.maxRunSpeed);
}
2023-04-09 21:59:32 -07:00
2023-04-17 00:01:49 -07:00
void Start()
{
2023-04-17 16:47:12 -07:00
playerBehavior = this.gameObject.GetComponent<PlayerBehavior>();
2023-04-17 00:01:49 -07:00
this.rb = this.GetComponent<Rigidbody2D>();
stateController = GameObject.Find("StateController").GetComponent<StateController>();
2023-04-09 21:59:32 -07:00
}
void OnMove(InputValue value)
{
2023-04-17 00:01:49 -07:00
this.movement = value.Get<Vector2>();
//Debug.Log(this.movement);
}
void OnJump() {
if (IsGrounded()) {
rb.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
}
}
2023-04-17 00:01:49 -07:00
void FixedUpdate()
2023-04-09 21:59:32 -07:00
{
2023-04-17 00:01:49 -07:00
Run(1);
}
float AccelerationRate()
{
return this.runAcceleration / this.maxRunSpeed;
}
private void Run(float lerpAmount)
{
float targetSpeed = this.movement.x * this.maxRunSpeed;
float speedDiff = targetSpeed - this.rb.velocity.x;
forward = Mathf.Sign(speedDiff);
2023-04-17 00:01:49 -07:00
float accel = AccelerationRate() * snappiness;
2023-04-17 00:01:49 -07:00
float accelRate = (Mathf.Abs(targetSpeed) > 0.1) ? accel : -accel;
float velPower = 1.0f;
float move = Mathf.Pow(Mathf.Abs(speedDiff) * accelRate, velPower) * forward;
2023-04-17 00:01:49 -07:00
this.onGround = IsGrounded();
2023-04-17 00:01:49 -07:00
float frictionAmount = 0.5f;
// accelerate
2023-04-17 16:47:12 -07:00
if (onGround && (Mathf.Abs(this.movement.x) > 0.1f)) { // regular acceleration
this.rb.AddForce(move * Vector2.right, ForceMode2D.Force);
2023-04-17 16:47:12 -07:00
} else if (!onGround && (Mathf.Abs(this.movement.x) > 0.1f) && !playerBehavior.grapplingRope.isGrappling) { // while in air
this.rb.AddForce(move * Vector2.right * airSpeedMultiplier, ForceMode2D.Force);
2023-04-17 16:47:12 -07:00
} else if (!playerBehavior.grapplingRope.isGrappling) { // while grappling
this.rb.AddForce(move * Vector2.right * airSpeedMultiplier * airSpeedMultiplier, ForceMode2D.Force);
}
// decelerate until stopped
2023-04-17 16:47:12 -07:00
if (onGround && Mathf.Abs(this.movement.x) < 0.1f)
{
if (Mathf.Abs(rb.velocity.x) > 0.1f) {
float amount = Mathf.Min(
Mathf.Abs(this.rb.velocity.x),
Mathf.Abs(frictionAmount)
);
amount *= Mathf.Sign(this.rb.velocity.x);
this.rb.AddForce(-amount * Vector2.right * snappiness, ForceMode2D.Impulse);
} else {
this.rb.velocity = new Vector2(0, rb.velocity.y);
}
}
}
bool IsGrounded()
{
if (Physics2D.BoxCast(transform.position, boxSize, 0, -transform.up, maxDistanceFromGround, groundLayer))
2023-04-17 00:01:49 -07:00
{
return true;
2023-04-17 00:01:49 -07:00
}
return false;
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawCube(transform.position - transform.up * maxDistanceFromGround, boxSize);
2023-04-09 21:59:32 -07:00
}
}