Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
SnowSmasher

Taking Damage on collision with AI

Question

Hi, could anyone show me how my character can take damage on collision with the enemies in my game? I am using the Health Script that comes with Oma's FPS Kit. Thanks :)

Share this post


Link to post
Share on other sites

15 answers to this question

Recommended Posts


  • 1

You can use the basic OnCollision and SendMessage/BroadcastMessage functions to achieve this. 

Example :

* I dont know what OMA's script looks like but this is the standard way to handle it.
 

This goes on enemy, make it a new script.

Please login or register to see this code.

 

This goes in the Player health script

Please login or register to see this code.

 

So , to explain. 

The enemy comes in contact with the object tagged "Player" , then the enemy sends a message to the collided object to broadcast the TakeDamageDealt function , and sends the assigned damage amount. The function on the player health script then receives this and applies it to the health var. 

There are many ways to achieve your required effect but this should do the trick. 

Make sure both the player and enemy objects have something that can detect collision, Character controller, RigidBody or such - coupled with colliders.

Share this post


Link to post
Share on other sites
  • 1
Please login or register to see this quote.

Thank you so much for helping, but I'm getting an error that says Please login or register to see this attachment.

and I'm not sure how to fix it.

Edited by SnowSmasher

Share this post


Link to post
Share on other sites
  • 1

Check health script name on player and damage function.

Replace "PlayerDamageNew" with script name and "PlayerDamage" with function name.

Please login or register to see this code.
  • Like 1

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this code.

I don't use the FPS Kit so I don't know exactly what the damage class is. But everything is literally right in the kit to set up. I don't really know why you need help when there's an AI Script in the kit?

Share this post


Link to post
Share on other sites
  • 0

Thanks, but I have another problem. The enemies seem to be attacking way too fast. Is there a way to add intervals between attacks? For example, the enemy can attack once and then he won't deal damage again until he waits for a second. Could you also help with enemies detecting the player within a radius? For example, the enemy will run at the player if the player is within the radius. I already have the running animation set up, but the enemies will run at the player from across the map. I'm pretty new at Unity and scripting, so this is difficult for me. Thank you so much <3

Share this post


Link to post
Share on other sites
  • 0

Instead of triggers, better use Vector3.Distance():
Please login or register to see this link.
if(dist < 2.0) ApplyDamage(damage);

 

And for attack rate you can take code from any weapon script.

 

Also you can find AI code in A* tutorial for FPS Kit:

 

Share this post


Link to post
Share on other sites
  • 0

Sorry, but I'm not sure I understand as I am fairly new to scripting as I mentioned before.  Could you show me how I can apply if(dist < 2.0) ApplyDamage(damage); into the script that Johannes provided? Could you also add the attack rate into the script? 
 

Please login or register to see this quote.

I also wouldn't  want to use A* pathfinding as I already have Nav Mesh, which is easier and less time consuming for me.

Thank you :)

Share this post


Link to post
Share on other sites
  • 0

Sorry for asking for so much, but would you mind helping with the detection of a player within a certain radius and also a script for the enemy taking damage and dying? Thank you 

Share this post


Link to post
Share on other sites
  • 0

I'm pretty sure there's a script in OMA's kit that lets the enemy take damage, but I'm not sure how to use it. Could someone give me a script that lets the enemy take damage with the weapon scripts provided?

Please login or register to see this code.

How could I apply this script to my enemies? Thanks

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...