Implement player punch and crude damage system
This commit is contained in:
35
Assets/Scripts/Damageable.cs
Normal file
35
Assets/Scripts/Damageable.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
[RequireComponent(typeof(RespawnOnTriggerEnter))]
|
||||
public class Damageable : MonoBehaviour
|
||||
{
|
||||
public float force = 50f;
|
||||
public float damage = 0f;
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D collision)
|
||||
{
|
||||
if (collision.gameObject.CompareTag("Punch Hurtbox"))
|
||||
{
|
||||
print($"{name}: Ouch");
|
||||
Damage();
|
||||
Recoil(collision.transform.parent.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
private void Recoil(GameObject damageSource)
|
||||
{
|
||||
GetComponent<Rigidbody2D>().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up) * damage, ForceMode2D.Force);
|
||||
}
|
||||
|
||||
private void Damage()
|
||||
{
|
||||
damage += force;
|
||||
}
|
||||
|
||||
public void ResetDamage()
|
||||
{
|
||||
damage = 0f;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Damageable.cs.meta
Normal file
2
Assets/Scripts/Damageable.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6bac5d744efb2eb40be2220da2d52b35
|
||||
@@ -3,6 +3,11 @@ using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.Tilemaps;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
[RequireComponent(typeof(BoxCollider2D))]
|
||||
[RequireComponent(typeof(PlayerInput))]
|
||||
[RequireComponent(typeof(AnimationPlayer))]
|
||||
[RequireComponent(typeof(Punch))]
|
||||
public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
[Header("Ground Layers")]
|
||||
@@ -12,6 +17,7 @@ public class PlayerMovement : MonoBehaviour
|
||||
public float walkSpeed;
|
||||
public float walkSpeedFactor = 1f;
|
||||
public float maxSpeed = 5f;
|
||||
public float slowdownMultiplier = 10f;
|
||||
public float virtualAxisX;
|
||||
public float virtualButtonJump;
|
||||
public float virtualButtonJumpLastFrame;
|
||||
@@ -27,6 +33,9 @@ public class PlayerMovement : MonoBehaviour
|
||||
private Rigidbody2D body;
|
||||
private BoxCollider2D collide;
|
||||
private PlayerInput input;
|
||||
private AnimationPlayer animationPlayer;
|
||||
private Punch punch;
|
||||
|
||||
private bool jumpInputStillValid = false;
|
||||
private float lastTimeJumpPressed;
|
||||
|
||||
@@ -38,9 +47,6 @@ public class PlayerMovement : MonoBehaviour
|
||||
|
||||
private float lastTimeOnGround;
|
||||
|
||||
public bool animate;
|
||||
private AnimationPlayer animationPlayer;
|
||||
|
||||
private Vector3 positionLastFrame;
|
||||
|
||||
void Start()
|
||||
@@ -50,7 +56,8 @@ public class PlayerMovement : MonoBehaviour
|
||||
body = GetComponent<Rigidbody2D>();
|
||||
collide = GetComponent<BoxCollider2D>();
|
||||
input = GetComponent<PlayerInput>();
|
||||
if (animate) animationPlayer = GetComponent<AnimationPlayer>();
|
||||
animationPlayer = GetComponent<AnimationPlayer>();
|
||||
punch = GetComponent<Punch>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
@@ -71,7 +78,7 @@ public class PlayerMovement : MonoBehaviour
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (animate) Animate();
|
||||
Animate();
|
||||
}
|
||||
|
||||
private void Animate()
|
||||
@@ -107,6 +114,8 @@ public class PlayerMovement : MonoBehaviour
|
||||
|
||||
private void Jump()
|
||||
{
|
||||
if (!punch.cancelable) return;
|
||||
|
||||
if (virtualButtonJumpLastFrame == 1f)
|
||||
{
|
||||
jumpInputStillValid = true;
|
||||
@@ -146,19 +155,15 @@ public class PlayerMovement : MonoBehaviour
|
||||
|
||||
private void HorizontalMovement()
|
||||
{
|
||||
//body.linearVelocity = new Vector2(virtualAxisX * walkSpeed, body.linearVelocity.y);
|
||||
if (!punch.cancelable) return;
|
||||
|
||||
body.AddForce(new Vector2(virtualAxisX * walkSpeed * walkSpeedFactor, 0), ForceMode2D.Force);
|
||||
|
||||
if (Mathf.Abs(body.linearVelocityX) >= maxSpeed)
|
||||
{
|
||||
body.linearVelocity = new Vector2(Mathf.Sign(body.linearVelocityX) * maxSpeed, body.linearVelocity.y);
|
||||
body.AddForce(new Vector2(-Mathf.Sign(body.linearVelocityX) * (Mathf.Abs(body.linearVelocityX) - maxSpeed) * slowdownMultiplier, 0));
|
||||
}
|
||||
|
||||
//if (!IsPhysicallyGrounded())
|
||||
//{
|
||||
body.linearVelocityX *= walkSmooth;
|
||||
//}
|
||||
|
||||
if (transform.position == positionLastFrame && (input.actions.FindAction("Move").ReadValue<Vector2>().x == 0))
|
||||
{
|
||||
virtualAxisX = 0;
|
||||
@@ -213,4 +218,9 @@ public class PlayerMovement : MonoBehaviour
|
||||
boxCollider2D.bounds.center.y + (vertical * boxCollider2D.bounds.extents.y)
|
||||
);
|
||||
}
|
||||
|
||||
public void StopVelocity()
|
||||
{
|
||||
if (IsPhysicallyGrounded()) body.linearVelocity = Vector2.zero;
|
||||
}
|
||||
}
|
||||
|
||||
49
Assets/Scripts/Punch.cs
Normal file
49
Assets/Scripts/Punch.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
[RequireComponent(typeof(PlayerMovement))]
|
||||
[RequireComponent(typeof(PlayerInput))]
|
||||
[RequireComponent(typeof(AnimationPlayer))]
|
||||
public class Punch : MonoBehaviour
|
||||
{
|
||||
public bool cancelable = true;
|
||||
|
||||
[SerializeField] private BoxCollider2D hurtbox;
|
||||
|
||||
InputActionAsset actions;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
actions = GetComponent<PlayerInput>().actions;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (actions.FindAction("Punch").ReadValue<float>() == 1f)
|
||||
{
|
||||
if (!cancelable) return;
|
||||
GetComponent<AnimationPlayer>().Punch();
|
||||
DisableCancellation();
|
||||
}
|
||||
}
|
||||
|
||||
public void EnableHurtbox()
|
||||
{
|
||||
hurtbox.enabled = true;
|
||||
}
|
||||
|
||||
public void DisableHurtbox()
|
||||
{
|
||||
hurtbox.enabled = false;
|
||||
}
|
||||
|
||||
public void DisableCancellation()
|
||||
{
|
||||
cancelable = false;
|
||||
}
|
||||
|
||||
public void EnableCancellation()
|
||||
{
|
||||
cancelable = true;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Punch.cs.meta
Normal file
2
Assets/Scripts/Punch.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9ae332d743f1a42ce8ee7a958853bcc1
|
||||
@@ -23,6 +23,10 @@ public class RespawnOnTriggerEnter : MonoBehaviour
|
||||
{
|
||||
rb.linearVelocity = Vector2.zero;
|
||||
}
|
||||
if (TryGetComponent<Damageable>(out var damageable))
|
||||
{
|
||||
damageable.ResetDamage();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user