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

@@ -9,7 +9,7 @@ public class Launch : MonoBehaviour {
[SerializeField] private float horizSpeed;
[SerializeField] private float vertSpeed;
public void ThrowTambourine(int facing) {
public void ThrowTambourine(float facing) {
GameObject newTambourine = Instantiate(tambourine, this.gameObject.transform.position, this.gameObject.transform.rotation);
// multiply horizSpeed by facing if not using moving launch point
newTambourine.GetComponent<Rigidbody2D>().AddForce(new Vector2(horizSpeed * facing, vertSpeed), ForceMode2D.Impulse);

View File

@@ -22,14 +22,16 @@ public class PlayerBehavior : MonoBehaviour
[Header("Tambourine:")]
[SerializeField] private Launch launcher;
[HideInInspector] public bool hasTambourine = true;
GameObject tambourine;
[Header("Grappling:")]
[SerializeField] public Tutorial_GrapplingGun grapplingGun;
[SerializeField] public Tutorial_GrapplingRope grapplingRope;
private GameObject grappleSurface;
[Header("State Control:")]
private StateController stateController;
[Header("Controllers:")]
[SerializeField] private PlayerMovement playerController;
[SerializeField] private StateController stateController;
void Start()
@@ -42,59 +44,24 @@ public class PlayerBehavior : MonoBehaviour
void Update()
{
// jump
// if (Input.GetKeyDown(KeyCode.Space)) {
if (playerInput.actions["Jump"].WasPressedThisFrame() && IsGrounded())
{
_rb.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
}
// throw tambourine
// if (Input.GetKeyDown(KeyCode.K)) {
if (playerInput.actions["ThrowTambourine"].WasPressedThisFrame())
{
if (hasTambourine && !grapplingRope.isGrappling)
{
launcher.ThrowTambourine(forward);
hasTambourine = false;
}
ThrowTambourine();
}
// grapple
GameObject tambourine = GameObject.FindGameObjectWithTag("tambourine");
tambourine = GameObject.FindGameObjectWithTag("tambourine");
// if (Input.GetKeyDown(KeyCode.L)) {
if (playerInput.actions["Grapple"].WasPressedThisFrame())
{
if (tambourine != null)
{ // grapple to tambourine
if (!grapplingRope.isGrappling && tambourine.GetComponent<TambourineBehavior>().pinned)
{
grapplingGun.GrappleToTambourine(tambourine);
grapplingRope.isGrappling = true;
}
}
else
{
if (grappleSurface != null)
{
grapplingGun.GrappleToSurface(grappleSurface.transform.position);
grapplingRope.isGrappling = true;
}
}
AttemptGrapple();
}
// if (Input.GetKeyUp(KeyCode.L)) {
if (playerInput.actions["Grapple"].WasReleasedThisFrame())
{
if (tambourine != null && grapplingRope.isGrappling)
{
tambourine.GetComponent<TambourineBehavior>().DestroySelf();
}
grapplingGun.ReleaseGrapple();
}
// if (Input.GetKey(KeyCode.L)) {
if (playerInput.actions["Grapple"].IsPressed())
{
Debug.DrawRay(transform.position, new Vector2(0.500f * forward, 0.866f), Color.green);
LetGoOfGrapple();
}
}
@@ -111,52 +78,38 @@ public class PlayerBehavior : MonoBehaviour
}
}
// void FixedUpdate() {
// if (grapplingRope.isGrappling && _hInput != 0 && !IsGrounded()) {
// // print("grappling force");
// _rb.AddForce(new Vector2(_hInput * (airSpeed / 3), 0));
// } else if (_hInput != 0 && !IsGrounded()) {
// _rb.AddForce(new Vector2(_hInput * airSpeed, 0));
// } else if (_hInput != 0) {
// // print("normal movement");
// _rb.AddForce(new Vector2(_hInput * moveSpeed, 0));
// // _rb.velocity = new Vector2(_hInput * moveSpeed, _rb.velocity.y);
// }
// }
void ThrowTambourine() {
if (hasTambourine && !grapplingRope.isGrappling)
{
launcher.ThrowTambourine(forward);
hasTambourine = false;
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "grappleSurface")
{
grappleSurface = col.gameObject;
void AttemptGrapple() {
if (tambourine != null)
{ // grapple to tambourine
if (!grapplingRope.isGrappling && tambourine.GetComponent<TambourineBehavior>().pinned)
{
grapplingGun.GrappleToTambourine(tambourine);
grapplingRope.isGrappling = true;
}
}
else if (col.tag == "instaDeath")
else
{
stateController.ToggleDeathCanvas();
Destroy(this.gameObject);
if (grappleSurface != null)
{
grapplingGun.GrappleToSurface(grappleSurface.transform.position);
grapplingRope.isGrappling = true;
}
}
}
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);
void LetGoOfGrapple() {
if (tambourine != null && grapplingRope.isGrappling)
{
tambourine.GetComponent<TambourineBehavior>().DestroySelf();
}
grapplingGun.ReleaseGrapple();
}
}

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);
}
}

View File

@@ -14,11 +14,11 @@ public class StateController : MonoBehaviour {
}
public void RespawnPlayer() {
ToggleDeathCanvas();
SetDeathCanvasActive(false);
Instantiate(player, spawnPoint.transform.position, spawnPoint.transform.rotation);
}
public void ToggleDeathCanvas() {
deathCanvas.SetActive(!deathCanvas.activeSelf);
public void SetDeathCanvasActive(bool activeState) {
deathCanvas.SetActive(activeState);
}
}