Implement player punch and crude damage system

This commit is contained in:
RochesterX
2025-01-17 19:46:17 -05:00
parent 0824f3c99a
commit 7a646ca1ea
10 changed files with 1646 additions and 240 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -12,11 +12,12 @@ GameObject:
- component: {fileID: 4642513082199340961}
- component: {fileID: 75167067980600170}
- component: {fileID: 6392580824388157607}
- component: {fileID: 5190430079571461372}
- component: {fileID: 2787587001401147982}
- component: {fileID: 5200627265685207284}
- component: {fileID: 1169583671018505929}
- component: {fileID: 2344147968324054325}
- component: {fileID: 8121950008105218343}
- component: {fileID: 5190430079571461372}
m_Layer: 0
m_Name: Bones
m_TagString: Untagged
@@ -37,6 +38,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 2063840941502744053}
- {fileID: 7290594200745798336}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@@ -58,6 +60,7 @@ MonoBehaviour:
walkSpeed: 5
walkSpeedFactor: 30
maxSpeed: 5
slowdownMultiplier: 40
virtualAxisX: 0
virtualButtonJump: 0
virtualButtonJumpLastFrame: 0
@@ -69,7 +72,6 @@ MonoBehaviour:
jumpLenience: 0.1
timeUnableToBeDeclaredNotJumping: 0.1
groundCheckDistance: 0.1
animate: 1
--- !u!114 &75167067980600170
MonoBehaviour:
m_ObjectHideFlags: 0
@@ -100,6 +102,34 @@ MonoBehaviour:
state: 0
backwards: 0
clip: {fileID: 7400000, guid: 590f229c565f34c8296a875497c76553, type: 2}
--- !u!114 &5190430079571461372
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1375850422501237868}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9ae332d743f1a42ce8ee7a958853bcc1, type: 3}
m_Name:
m_EditorClassIdentifier:
cancelable: 1
hurtbox: {fileID: 2982388803569647282}
--- !u!114 &2787587001401147982
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1375850422501237868}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6bac5d744efb2eb40be2220da2d52b35, type: 3}
m_Name:
m_EditorClassIdentifier:
force: 200
damage: 0
--- !u!114 &5200627265685207284
MonoBehaviour:
m_ObjectHideFlags: 0
@@ -225,18 +255,6 @@ BoxCollider2D:
m_AutoTiling: 0
m_Size: {x: 0.4505486, y: 1.9777327}
m_EdgeRadius: 0
--- !u!114 &5190430079571461372
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1375850422501237868}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9ae332d743f1a42ce8ee7a958853bcc1, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1648017802882051395
GameObject:
m_ObjectHideFlags: 0
@@ -1586,3 +1604,81 @@ BoxCollider2D:
m_AutoTiling: 0
m_Size: {x: 0.0625, y: 0.5}
m_EdgeRadius: 0
--- !u!1 &8302541871276031440
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2063840941502744053}
- component: {fileID: 2982388803569647282}
m_Layer: 0
m_Name: Punch Hurtbox
m_TagString: Punch Hurtbox
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &2063840941502744053
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8302541871276031440}
serializedVersion: 2
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 1.4, y: 0.3, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 7009259675412950971}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!61 &2982388803569647282
BoxCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8302541871276031440}
m_Enabled: 0
serializedVersion: 3
m_Density: 1
m_Material: {fileID: 0}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_LayerOverridePriority: 0
m_ForceSendLayers:
serializedVersion: 2
m_Bits: 4294967295
m_ForceReceiveLayers:
serializedVersion: 2
m_Bits: 4294967295
m_ContactCaptureLayers:
serializedVersion: 2
m_Bits: 4294967295
m_CallbackLayers:
serializedVersion: 2
m_Bits: 4294967295
m_IsTrigger: 1
m_UsedByEffector: 0
m_CompositeOperation: 0
m_CompositeOrder: 0
m_Offset: {x: -0.5045183, y: 0}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0, y: 0}
oldSize: {x: 0, y: 0}
newSize: {x: 0, y: 0}
adaptiveTilingThreshold: 0
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
m_Size: {x: 2.0090363, y: 0.57673347}
m_EdgeRadius: 0

View File

@@ -1,22 +0,0 @@
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(PlayerInput))]
[RequireComponent(typeof(AnimationPlayer))]
public class Punch : MonoBehaviour
{
InputActionAsset actions;
private void Start()
{
actions = GetComponent<PlayerInput>().actions;
}
private void Update()
{
if (actions.FindAction("Punch").ReadValue<float>() == 1f)
{
GetComponent<AnimationPlayer>().Punch();
}
}
}

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

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6bac5d744efb2eb40be2220da2d52b35

View File

@@ -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
View 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;
}
}

View File

@@ -23,6 +23,10 @@ public class RespawnOnTriggerEnter : MonoBehaviour
{
rb.linearVelocity = Vector2.zero;
}
if (TryGetComponent<Damageable>(out var damageable))
{
damageable.ResetDamage();
}
}
}
}