Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheGalacticShaman · Jan 26, 2019 at 09:43 AM · aiscripting beginnernpcstate machinefighting

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 ! :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheGalacticShaman · Jan 26, 2019 at 12:14 PM 0
Share

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 ?

avatar image Nocktion · Jan 26, 2019 at 02:08 PM 1
Share

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.

avatar image Nocktion · Jan 26, 2019 at 02:19 PM 1
Share

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.

avatar image TheGalacticShaman Nocktion · Jan 27, 2019 at 03:27 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

172 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges