- Home /
Making a fighter respond to being attacked
Hey guys, so for a little while I have been developing a small fighting game prototype.
For this prototype, I am trying to make a fighting game where multiple ai in teams fight each other (melee combat). So far, I have a functioning state machine system on each fighter that seems to work relatively well, right now they select a target, move towards it, attack until it is dead, then select another target until all enemies are dead.
However, this isn't exactly the thrilling fight I want to have in my game, as I want to have the fighter defending from an attack block or dodge. Im just struggling to implement this as a reaction.
I know how the block and dodge will function (negate damage and play animation/ move fighter out of the way and animation), but my problem lays within HOW these reactions should be initiated.
How would you suggest I make my defender know it should be blocking ? This problem becomes larger when you consider that multiple targets could be attacking a single fighter at any one time.
Any help/insight would be greatly appreciated ! :)
Answer by Nocktion · Jan 26, 2019 at 11:55 AM
Before the attacker hits the defender e. g. 1s before he attacks send a message to the target (you can use a coroutine for that). So he will now he will be attacked in a second. Oh and if I were you I would add a random to the blocking mechanism, so there would be a chance that the defender can't block in time. Happy coding
Yeah that sounds good :) how would you suggest handling the actual message trigger ? Would the attacker be accessing a variable inside the defenders script, or is there a better way to handle this ? I saw that unity has an actual "message" function in c#, is that what you mean ?
The built-in Send$$anonymous$$essage method works, but I don't recommend it for performance reasons. I think if the attacker is close enough to the defender you can do a quick raycast and store the hit in a variable. After that you get the defenders script from hit.transform and call a Block method. After the method call you launch a coroutine and that waits a second and then calls the local attack method. I'll write an example code in a moment to make this more understandable.
Remember that this is just a part of a script and I don't know the names of your scripts. The logic for checking and messaging: private RaycastHit hit;
//Here comes the method probably Update() and other codes
if (distance To Enemy < attack Distance) {
if (Physics.Raycast(transform.position, dirToEnemy, out hit)
{
hit.transform.GetComponent(DefenderScript).Block();
StartCoroutine(WaitToAttack());
}
}
The coroutine:
private IEnumerator WaitToAttack() {
yield return new WaitForSeconds(1f);
Attack() ;
}
And in the Block method:
public void Block() {
if (Random.value < 0.5f)
return;
//Blocking logic
}
This adds a random so there is a chance that the defender can't block the current hit. Hope it helps.
Wow thank you so much ! yes I think this approach is much better than what I currently have, I appreciate you taking the time to help :)
Your answer
Follow this Question
Related Questions
Making an AI for an NPC 1 Answer
A set of actions 1 Answer
Help needed for a dialogue/speech System 1 Answer
Making AI that can navigate through a city? 3 Answers
Moving from one position to another 3 Answers