Let us make the player react to the enemy's attacks by changing the behavior of the player. In the playerScript
, add the following line of code. At the top of the class, create a new public
variable of type GameObject
and call it enemy
:
public GameObject enemy;
Next, at the start of the Update
function, get the Animator
component of the enemy:
Animator eAnim = enemy.GetComponent<Animator>();
Next, after the attacking function in the update, add the following:
// Getting Hit if (eAnim.GetBool("tIsPunching")){ if (anim.GetBool("bIsDefending") == false){ //Debug.Log("I got hit"); anim.SetTrigger("tGotHit"); } }
Here, we check if the enemy is punching. If the enemy is punching and the player is not defending, then we set the gotHit
trigger to be true
.
Drag theDude
character to the enemy script component as shown in the following screenshot:
Similarly, in the enemy script add a public GameObject variable...