- Home /
,Capture user input as per animation state and then play animations accordingly
Objective
To enable a user to respond to left or right attacks. If attacks are responded in time, then the attack is blocked. Otherwise, if not responded in time or responded in wrong direction (that is attack from left but user pressed right) then user will take a Hit. Essentially, I guess this is what i want:
First, I want to trigger animation events (of left or right attack).
Then, want to capture user input ('A' or 'D')
Then, I wish to check if user input according to attack (for left attack need to press 'A', for right attack 'D' key.
Lastly, play blocked or hit accordingly.
I am very new to Unity, and any help, any link will be very much appreciated.
Process so far:
Made a GameObject. Attached an animator using single frame animation events (basically just images, with no loop time). And attached the following script on the GameObject. Also made my Fixed time update to 1s (in Unity project settings). Following is animator setting {L,R,B,I stand for Left, right, blocked,Idle}
Problem:
During run, the animation goes to idle first (default state), then immediately to Hit. I tried pressing A (or even D) for multiple FixedUpdates and in some of them even managed to Block (not Hit), but almost always it jumps so quickly to Hit state that I can't react to it.
Also I would not like to use FixedUpdate of 1s, becoz i would like to implement it on animating characters and there time intervals will be much shorter. But using Update method, there are far more runs per second than i can visualise on screen and respond to.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
Animator _enemyAnimator;
[SerializeField] float rxnTime=0.5f;
int attackLorR;
void Start()
{
_enemyAnimator = GetComponent<Animator>();
}
void Update()
{
LorRAttack();
if (attackLorR == 1)
{ _enemyAnimator.SetTrigger("isLeft"); Debug.Log("LeftTriggered"); }
else if (attackLorR == 2)
{ _enemyAnimator.SetTrigger("isRight"); Debug.Log("RightTriggered"); }
StartCoroutine(WaitForReaction(rxnTime));
{
if (Input.GetKey(KeyCode.A) && attackLorR == 1)
{
_enemyAnimator.SetTrigger("isBlocked");
Debug.Log("SUCCESS: Left Attack Blocked");
}
else if (Input.GetKey(KeyCode.D) && attackLorR == 2)
{
_enemyAnimator.SetTrigger("isBlocked");
Debug.Log("SUCCESS: Right Attack Blocked");
}
else if (Input.GetKey(KeyCode.A) && attackLorR == 2)
{
_enemyAnimator.SetTrigger("isHit");
Debug.Log("FAILED: Right Attack Missed");
}
else if (Input.GetKey(KeyCode.D) && attackLorR == 1)
{
_enemyAnimator.SetTrigger("isHit");
Debug.Log("FAILED: Left Attack Missed");
}
else if ((!Input.GetKey(KeyCode.D) || !Input.GetKeyDown(KeyCode.D)) &&
(attackLorR == 1 || attackLorR == 2))
{
_enemyAnimator.SetTrigger("isHit");
Debug.Log("FAILED: NoKeyPress");
}
}
}
private void LorRAttack()
{
float num = Random.Range(0.0f, 2.0f);
if (num < 1f) attackLorR = 1;
else attackLorR = 2;
Debug.Log(attackLorR);
}
IEnumerator WaitForReaction(float rxnTime)
{
yield return new WaitForSeconds(rxnTime);
}
}
Thanks a lot for reading this far