Unity Platform 08 Player Combat
===
###### tags: `platform`
## List
1. [Set Animator Controller](#SAC)
2. [Combat Basic with Player Cycle](#CBP)
3. [Hit Box & Player conditions](#HB)
## Content
### Set Animator Controller<a id="SAC"/>
Animation Controller must be set carefully.<br>
set Transition and duration for starting & end of animtion.<br>In Script, `Animator.set~()` control state of animation.<br>Ref [This](https://www.youtube.com/watch?v=YaXcwc5Evjk&list=PLy78FINcVmjA0zDBhLuLNL1Jo6xNMMq-W&index=10) 1:40 ~ <br><br>
### Combat Basic with Player Cycle<a id="CBP"/>
Technic is same for other actions.<br>Check input, check action, and active, finish.<br>But Script is isolated.<br>Need state below
```csharp=
private bool gotInput; // need for inputBotton is pushed
private bool iSAttacking; // need for Animation & condition is Attacking now.
private bool isFirstAttack; // need for Animation & later...
```
<br>
First, check input correct & set condition
```csharp=
private void CheckCombatInput()
{
if (Input.GetMouseButton(0))
{
if (combatEnabled)
{
// Attempt combat
gotInput = true;
lastInputTime = Time.time;
}
}
}
```
<br>
Second, check state and condition updating.
```csharp=
private void CheckAttacks()
{
if (gotInput)
{
//Perform Attack1
if (!iSAttacking)
{
gotInput = false;
iSAttacking = true;
isFirstAttack = !isFirstAttack;
anim.SetBool("attack1", true);
anim.SetBool("fistAttack", isFirstAttack);
anim.SetBool("isAttacking", iSAttacking);
}
}
if (Time.time >= lastInputTime + inputTimer)
{
// Wait for new input (new attack possible)
gotInput = false;
}
}
```
<br>
Last, finish attack.<br>
Set Animtion state.
```csharp=
private void FinishAttack1()
{
iSAttacking = false;
anim.SetBool("isAttacking", iSAttacking);
anim.SetBool("attack1", false);
}
```
<br><br>
### Hit Box & Player conditions<a id="HB"/>
Attack also have to check objs collider.<br>Use same technic grouding, check all objs attached.<br>Set it ~ing of animation.
```csharp=
private void CheckAttackHitBox()
{
Collider2D[] detectedObjects = Physics2D.OverlapCircleAll(attackHitBoxPos.position, attackRadius, whttIsDamageable);
foreach (Collider2D collider in detectedObjects)
{
collider.transform.parent.SendMessage("Damage", attackDamage);
//Instantiate hit particle;
}
}
```
<br>
And must change player's conditions, especially flip.<br>For Changing, make public function, `Enable / DisableFlip()`<br>they are called by attack animation's start and end.
```csharp=
public void DisableFlip()
{
canFlip = false;
}
public void EnableFlip()
{
canFlip = true;
}
```