reconfigured collision

also made some adjustments to player behavior and controller
This commit is contained in:
slevy14
2023-04-17 15:22:24 -07:00
parent fb8732f820
commit 275763628b
19 changed files with 533 additions and 928 deletions

View File

@@ -7,7 +7,11 @@ public class PlayerMovement : MonoBehaviour
public float maxRunSpeed;
public float runAcceleration;
public float snappiness = 1;
public float jumpSpeed;
[Range(0,1)] public float airSpeedMultiplier;
private bool onGround = false;
private float forward = 1;
float hangTimeThreshold = 0.1f;
float hangTimeAccel = 0;
@@ -15,6 +19,22 @@ public class PlayerMovement : MonoBehaviour
private Vector2 movement = Vector2.zero;
public LayerMask groundLayer;
public Vector2 boxSize;
public float maxDistanceFromGround;
[Header("Grappling:")]
[SerializeField] public Tutorial_GrapplingGun grapplingGun;
[SerializeField] public Tutorial_GrapplingRope grapplingRope;
private GameObject grappleSurface;
[Header("Tambourine:")]
[SerializeField] private Launch launcher;
[HideInInspector] public bool hasTambourine = true;
[Header("State Control:")]
[SerializeField] private StateController stateController;
void OnValidate()
{
this.runAcceleration = Mathf.Clamp(runAcceleration, 0.1f, this.maxRunSpeed);
@@ -23,6 +43,7 @@ public class PlayerMovement : MonoBehaviour
void Start()
{
this.rb = this.GetComponent<Rigidbody2D>();
stateController = GameObject.Find("StateController").GetComponent<StateController>();
}
void OnMove(InputValue value)
@@ -31,6 +52,12 @@ public class PlayerMovement : MonoBehaviour
//Debug.Log(this.movement);
}
void OnJump() {
if (IsGrounded()) {
rb.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
}
}
void FixedUpdate()
{
Run(1);
@@ -46,29 +73,76 @@ public class PlayerMovement : MonoBehaviour
float targetSpeed = this.movement.x * this.maxRunSpeed;
float speedDiff = targetSpeed - this.rb.velocity.x;
forward = Mathf.Sign(speedDiff);
float accel = 0.5f;
float accel = AccelerationRate() * snappiness;
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);
float move = Mathf.Pow(Mathf.Abs(speedDiff) * accelRate, velPower) * forward;
this.rb.AddForce(move * Vector2.right, ForceMode2D.Force);
this.onGround = IsGrounded();
float frictionAmount = 0.5f;
this.onGround = true;
// accelerate
if (onGround && (Mathf.Abs(this.movement.x) > 0.1f)) {
this.rb.AddForce(move * Vector2.right, ForceMode2D.Force);
} else if (!onGround && (Mathf.Abs(this.movement.x) > 0.1f)) {
this.rb.AddForce(move * Vector2.right * airSpeedMultiplier, ForceMode2D.Force);
}
if (this.onGround && (Mathf.Abs(this.movement.x) < 0.1f))
// decelerate until stopped
if (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);
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);
}
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "grappleSurface")
{
grappleSurface = col.gameObject;
}
else if (col.tag == "instaDeath")
{
this.stateController.SetDeathCanvasActive(true);
Destroy(this.gameObject);
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.tag == "grappleSurface")
{
grappleSurface = null;
}
}
bool IsGrounded()
{
if (Physics2D.BoxCast(transform.position, boxSize, 0, -transform.up, maxDistanceFromGround, groundLayer))
{
return true;
}
return false;
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawCube(transform.position - transform.up * maxDistanceFromGround, boxSize);
}
}