better fighting

This commit is contained in:
Xaver Drabik 2024-12-23 21:34:57 +01:00
parent 68a1670d37
commit d6b3c8cbfb
10 changed files with 251 additions and 66 deletions

1
.gitignore vendored
View File

@ -159,3 +159,4 @@ twinStickCrawler/obj/Debug
twinStickCrawler/UserSettings
twinStickCrawler/Temp
twinStickCrawler/build
twinStickCrawler/twinStickCrawler.sln.DotSettings

View File

@ -62,6 +62,13 @@ ModelImporter:
floatParameter: 0
intParameter: 0
messageOptions: 0
- time: 1
functionName: AnimationEnd
data:
objectReferenceParameter: {instanceID: 0}
floatParameter: 0
intParameter: 0
messageOptions: 0
transformMask: []
maskType: 3
maskSource: {instanceID: 0}

View File

@ -55,6 +55,20 @@ ModelImporter:
bodyMask: 01000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000
curves: []
events:
- time: 0
functionName: StartDamage
data:
objectReferenceParameter: {instanceID: 0}
floatParameter: 0
intParameter: 0
messageOptions: 0
- time: 0.10427807
functionName: EndDamage
data:
objectReferenceParameter: {instanceID: 0}
floatParameter: 0
intParameter: 0
messageOptions: 0
- time: 0.2185567
functionName: LeftReset
data:
@ -62,6 +76,13 @@ ModelImporter:
floatParameter: 0
intParameter: 0
messageOptions: 0
- time: 0.7237113
functionName: AnimationEnd
data:
objectReferenceParameter: {instanceID: 0}
floatParameter: 0
intParameter: 0
messageOptions: 0
transformMask: []
maskType: 3
maskSource: {instanceID: 0}

View File

@ -55,6 +55,20 @@ ModelImporter:
bodyMask: 01000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000
curves: []
events:
- time: 0
functionName: StartDamage
data:
objectReferenceParameter: {instanceID: 0}
floatParameter: 0
intParameter: 0
messageOptions: 0
- time: 0.15775403
functionName: EndDamage
data:
objectReferenceParameter: {instanceID: 0}
floatParameter: 0
intParameter: 0
messageOptions: 0
- time: 0.23529406
functionName: RightReset
data:
@ -62,6 +76,13 @@ ModelImporter:
floatParameter: 0
intParameter: 0
messageOptions: 0
- time: 0.75051546
functionName: AnimationEnd
data:
objectReferenceParameter: {instanceID: 0}
floatParameter: 0
intParameter: 0
messageOptions: 0
transformMask: []
maskType: 3
maskSource: {instanceID: 0}

View File

@ -920,6 +920,59 @@ PrefabInstance:
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: a0b56cb96370346c29fa346cedbbc0f4, type: 3}
--- !u!1 &703790483
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 703790484}
- component: {fileID: 703790485}
m_Layer: 3
m_Name: Hurtbox
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &703790484
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 703790483}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0.488}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1931938465}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!65 &703790485
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 703790483}
m_Material: {fileID: 0}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_LayerOverridePriority: 0
m_IsTrigger: 1
m_ProvidesContacts: 0
m_Enabled: 1
serializedVersion: 3
m_Size: {x: 1, y: 2.0183148, z: 0.9697957}
m_Center: {x: 0, y: 0.0086295605, z: -0.015102148}
--- !u!1 &745541349
GameObject:
m_ObjectHideFlags: 0
@ -2265,12 +2318,13 @@ Transform:
m_GameObject: {fileID: 1931938463}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 2.55, z: 0}
m_LocalPosition: {x: 0, y: 2.55, z: 1.156}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 129091607}
- {fileID: 1965505412}
- {fileID: 703790484}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!136 &1931938467
@ -2451,10 +2505,15 @@ MonoBehaviour:
dashTime: 0.2
dashCooldownTime: 1
dashTrail: {fileID: 1931938469}
enemyLayer:
attackMoveSpeed: 1.5
_enemyLayer:
serializedVersion: 2
m_Bits: 64
attackResetTime: 2
_hurtBox: {fileID: 703790485}
_basedamage: 15
_availableAttacks:
- Left
- Right
--- !u!1 &1965505411
GameObject:
m_ObjectHideFlags: 0

View File

@ -30,8 +30,11 @@ public void TriggerAnimation(string triggerName)
{
_animator.SetTrigger(triggerName);
}
public event Action OnStartDamage;
public event Action OnEndDamage;
public event Action OnReset;
public event Action OnAnimationEnd;
void LeftReset(AnimationEvent animationEvent)
{
OnReset?.Invoke();
@ -44,4 +47,17 @@ void KickReset(AnimationEvent animationEvent)
{
OnReset?.Invoke();
}
void StartDamage(AnimationEvent animationEvent)
{
OnStartDamage?.Invoke();
}
void EndDamage(AnimationEvent animationEvent)
{
OnEndDamage?.Invoke();
}
void AnimationEnd(AnimationEvent animationEvent)
{
OnAnimationEnd?.Invoke();
}
}

View File

@ -1,4 +1,4 @@
using System.Collections;
using System;
using UnityEngine;
public class Player : MonoBehaviour
@ -12,14 +12,18 @@ public class Player : MonoBehaviour
private Animator _animator;
[Header("Camera Options")]
[SerializeField] private Vector3 camOffset;
[SerializeField] private float cameraFollowSpeed = 2.5f;
[SerializeField]
private Vector3 camOffset;
[SerializeField]
private float cameraFollowSpeed = 2.5f;
[Header("Player Options")]
[Range(0f, 10f)]
public float moveSpeed = 5f;
[SerializeField]
private float moveSpeed = 6.5f;
[Range(0f, 100f)]
public float animationSpeed = 30f;
[SerializeField]
private float animationSpeed = 10f;
[Header("Enemy Snapping")]
[SerializeField]
@ -40,8 +44,17 @@ public class Player : MonoBehaviour
public TrailRenderer dashTrail;
[Header("Attacking")]
public LayerMask enemyLayer;
public float attackResetTime;
[Range(0f, 10f)]
[SerializeField]
private float attackMoveSpeed = 2f;
[SerializeField]
private LayerMask _enemyLayer;
[SerializeField]
private Collider _hurtBox;
[SerializeField]
private float _basedamage;
[SerializeField]
private string[] _availableAttacks;
private Camera _camera;
private CharacterController _controller;
@ -53,8 +66,12 @@ public class Player : MonoBehaviour
private PlayerAttack _playerAttack;
private CameraMove _cameraMove;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Awake()
{
SetupGame();
}
void SetupGame()
{
camOffset.z = camOffset.x;
@ -69,7 +86,7 @@ void Awake()
_animate = _animator.gameObject.AddComponent<Animate>();
_animate.initialize(_animator);
_playerAttack = gameObject.AddComponent<PlayerAttack>();
_playerAttack.initialize(_inputManager, enemyLayer, _animate, attackResetTime);
_playerAttack.initialize(_inputManager, _enemyLayer, _animate, _hurtBox, _availableAttacks);
_playerMove = gameObject.AddComponent<PlayerMove>();
_playerMove.initialize(_controller, dashTrail);
@ -81,29 +98,18 @@ void Update()
{
Vector3 moveInput = new Vector3(_inputManager.moveInput.x, 0f , _inputManager.moveInput.y);
bool attacking = _playerAttack.Attack();
float movementSpeed = attacking ? attackMoveSpeed : moveSpeed;
Vector3 moveDirection = _playerMove.GetMoveDirection(moveInput, _camera.transform);
Vector3 lookTarget = _playerRotate.LookTarget(moveDirection);
moveDirection += _playerMove.Gravity();
_playerMove.Dash(dashTime, dashCooldownTime, dashSpeed, _inputManager.sprintPerformed);
_playerMove.ApplyMovement(moveDirection, lookTarget, moveSpeed);
_cameraMove.MoveCamera();
Vector3 aimTarget = _playerAimRotate.AimTarget(minAimDistance, maxAimDistance, _camera, _inputManager.aimInput);
Vector3 moveDirection = _playerMove.GetMoveDirection(moveInput, _camera.transform) + _playerMove.Gravity();
Vector3 lookTarget = attacking ? aimTarget : _playerRotate.LookTarget(moveDirection); // if player is attacking look towards attack direction.
_playerMove.Dash(dashTime, dashCooldownTime, dashSpeed, _inputManager.sprintPerformed, attacking);
_playerMove.ApplyMovement(moveDirection, lookTarget, movementSpeed);
_cameraMove.MoveCamera();
_playerAimRotate.ApplyRotation(aimTarget);
_playerAttack.Attack();
_animate.AnimateState("Walk", moveInput.magnitude, animationSpeed);
/*
//360 Char Animation
//Vector3 localMoveDirection = transform.InverseTransformDirection(moveDirection);
//_animate.AnimateState("X", localMoveDirection.x, animationSpeed);
//_animate.AnimateState("Y", localMoveDirection.z, animationSpeed);
//
*/
}
}

View File

@ -1,5 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;
@ -9,49 +11,100 @@ public class PlayerAttack : MonoBehaviour
private InputManager _inputManager;
private LayerMask _enemyLayermask;
private Animate _animate;
private float _attackResetTime;
public void initialize(InputManager inputManager, LayerMask enemyLayermask, Animate animate, float attackResetTime)
private Collider _hurtbox;
private string[] _availableAttacks;
public void initialize(InputManager inputManager, LayerMask enemyLayermask, Animate animate, Collider hurtbox, string[] availableAttacks)
{
_inputManager = inputManager;
_enemyLayermask = enemyLayermask;
_hurtbox = hurtbox;
_animate = animate;
animate.OnReset += HandleReset;
_attackResetTime = attackResetTime;
_availableAttacks = availableAttacks;
OnEnable();
}
private bool _canAttack = true;
private int _attack = 1;
public void Attack()
#region Event Setup
private void OnEnable()
{
if (_canAttack && _inputManager.attackPerformed)
if (_animate != null)
{
if (_attack >= 3 || _animate.IsCurrentAnimation("Blend Tree"))
{
_attack = 1;
}
switch (_attack)
{
case 1:
_animate.TriggerAnimation("Left");
print("left");
break;
case 2:
_animate.TriggerAnimation("Right");
print("right");
break;
}
_canAttack = false;
_attack++;
_animate.OnReset += _onResetHandler;
_animate.OnStartDamage += _onStartDamageHandler;
_animate.OnEndDamage += _onEndDamageHandler;
_animate.OnAnimationEnd += _onAnimationEndHandler;
}
}
public void HandleReset()
private void OnDisable()
{
_animate.OnReset -= _onResetHandler;
_animate.OnStartDamage -= _onStartDamageHandler;
_animate.OnEndDamage -= _onEndDamageHandler;
_animate.OnAnimationEnd -= _onAnimationEndHandler;
}
private void _onResetHandler()
{
print("reset");
_canAttack = true;
}
private void _onAnimationEndHandler()
{
print("tf");
_attacking = false;
}
private void _onStartDamageHandler()
{
StartCoroutine(DealDamage());
}
private void _onEndDamageHandler()
{
_damageEnded = true;
}
#endregion
private bool _attacking=false;
private bool _canAttack = true;
private int _attack = 0;
public bool Attack()
{
if (_canAttack && _inputManager.attackPerformed)
{
if (_attack >= 2 || _animate.IsCurrentAnimation("Blend Tree"))
{
_attack = 0;
}
_animate.TriggerAnimation(_availableAttacks[_attack]);
_canAttack = false;
_attack++;
_attacking = true;
}
return _attacking;
}
private bool _damageEnded = true;
IEnumerator DealDamage()
{
_damageEnded = false;
while (!_damageEnded)
{
Collider[] hitColliders = Physics.OverlapBox(_hurtbox.bounds.center, _hurtbox.bounds.extents, Quaternion.identity, _enemyLayermask);
for (int i = 0; i < hitColliders.Length; i++)
{
//damage code
//hitColliders
}
yield return null;
}
}
}

View File

@ -43,9 +43,9 @@ public Vector3 Gravity()
private bool _dashing = false;
private bool _dashPossible = true;
public void Dash(float dashTime, float dashCooldownTime, float dashSpeed, bool sprintPerformed)
public void Dash(float dashTime, float dashCooldownTime, float dashSpeed, bool sprintPerformed, bool attacking)
{
if (sprintPerformed && !_dashing && _dashPossible)
if (sprintPerformed && !attacking && !_dashing && _dashPossible)
{
StartCoroutine(DashCoroutine(dashTime, dashCooldownTime, dashSpeed));
}

View File

@ -4,6 +4,7 @@ public class PlayerRotate : MonoBehaviour
{
public Vector3 LookTarget(Vector3 moveDir)
{
moveDir.y = 0;
return transform.position + moveDir;
}
}