Progress on movement controller
This commit is contained in:
@@ -1,31 +1,74 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class PlayerController : MonoBehaviour
|
||||
public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
private Rigidbody2D rb;
|
||||
|
||||
Vector2 movementValue;
|
||||
[SerializeField]
|
||||
float speed;
|
||||
public float maxRunSpeed;
|
||||
public float runAcceleration;
|
||||
private bool onGround = false;
|
||||
|
||||
float hangTimeThreshold = 0.1f;
|
||||
float hangTimeAccel = 0;
|
||||
float hangTimeSpeed = 0;
|
||||
|
||||
private Vector2 movement = Vector2.zero;
|
||||
|
||||
void OnValidate()
|
||||
{
|
||||
this.runAcceleration = Mathf.Clamp(runAcceleration, 0.1f, this.maxRunSpeed);
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
Rigidbody2D rb;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
this.rb = this.GetComponent<Rigidbody2D>();
|
||||
}
|
||||
|
||||
void OnMove(InputValue value)
|
||||
{
|
||||
this.movementValue = value.Get<Vector2>() * speed;
|
||||
this.movement = value.Get<Vector2>();
|
||||
//Debug.Log(this.movement);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
void FixedUpdate()
|
||||
{
|
||||
this.rb.AddForce(new Vector3(this.movementValue.x, 0, this.movementValue.y));
|
||||
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;
|
||||
|
||||
float accel = 0.5f;
|
||||
|
||||
float accelRate = (Mathf.Abs(targetSpeed) > 0.1) ? accel : -accel;
|
||||
|
||||
float velPower = 1.0f;
|
||||
|
||||
float move = Mathf.Pow(Mathf.Abs(speedDiff) * accelRate, velPower) * Mathf.Sign(speedDiff);
|
||||
|
||||
this.rb.AddForce(move * Vector2.right, ForceMode2D.Force);
|
||||
|
||||
float frictionAmount = 0.5f;
|
||||
|
||||
this.onGround = true;
|
||||
|
||||
if (this.onGround && (Mathf.Abs(this.movement.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, ForceMode2D.Impulse);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user