x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

How do I make spawn projectiles deal damage to the player?

So I tried my best looking up a solution and didn't have any luck, I recently made enemy character blueprints and I want the projectiles they spawn to deal damage to the player I already have it set up that I can kill them however I just can't figure out how to get them to deal damage to me.

Currently, I can take damage from my melee attackers as I have a blueprint in my character that will make the player take damage on component hit however because my ranged enemies are spawning brand new projectiles I don't know how I would go about making a script for those newly projectiles which don't have an origin besides the scene I put in for the projectile to spawn from I'm fairly new to scripting so any help would be appreciated.

Product Version: UE 4.20
Tags:
ehealth.png (113.3 kB)
phealth.png (127.2 kB)
more ▼

asked Feb 03 '19 at 10:59 PM in Blueprint Scripting

avatar image

Shalacka
3 1 1

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

it appears your using the first person template right? if thats the case the projectile your spawning is its own actor. which means you could just expand your current system by opening the projectile actor and adding in a on hit event which casts the hit actor to your players class (firstpersoncharacter) then drag off the return value and set the health.

personally i dont like that approach and believe you should make use of the in built damage system. with the built in system each character would have two events in your case, event any damage and event begin overlap. the event any damage handles any damage which is incoming to the character. the begin overlap for your example would be your attack and would make use of the apply damage node to do damage to the actors it overlaps.

below is an example of how your system could look (i didnt include the check for death since im lazy and you seem to know that bit). anyway as you can see in the picture we have event any damage which has a pin for damage incoming. we take that damage and subtract it from the health and thats all thats needed to receive damage. then theres your attack begin overlap, here you just need to use the apply damage node which only requires you to give it a actor and how much damage to apply. if the other actor doesnt have a event for receiving damage then its ignored without issue. so it basically is like saying i hit a thing i dont care what it is apply some damage, if the other thing doesnt know what to do with damage it doesnt do anything and just goes meh whatevs. ok getting a bit weird now but thats the basic idea and the two major nodes are built into the engine.

oh and this system can be easily applied to your projectile as well. on overlap or hit apply damage. thats all the script it would need.

alt text

capture.png (102.0 kB)
more ▼

answered Feb 04 '19 at 01:42 AM

avatar image

ThompsonN13
10.1k 33 10 23

avatar image Shalacka Feb 04 '19 at 01:51 AM

Thanks for such a quick reply, I wasn't expecting an answer so quickly. So with that any damage event, is that something I can just create in a blueprint via right-click and do I still need to create a health variable or is there somewhere built into UE that lets me set health to a blueprint? The health system you've shown me seems way easier to do than the one I made myself.

Also, how do you do those little nodes rather than directly connecting them to the next thing?

avatar image Shalacka Feb 04 '19 at 02:49 AM

So I went into unreal and tried what you done in yours from what I understood. I put the overlap apply damage in the FirstPersonProjectile since thats what my gunner and patroller "Spawnactor" does to shoot the player with and if I understood correct that's all the script I would need in there.

For the FPCharacter I followed what you done and continued on with the script, I applied that script to my enemy blueprints as well, both patroller and gunner (gunner is the one i uploaded but its the same for patroller). I'm assuming this all I needed to do for it to work however I don't seem to be taking any damage and I don't seem to be damaging them. alt text

alt text

alt text

avatar image ThompsonN13 Feb 04 '19 at 03:48 AM

from what i see script wise it looks right aside from the variable in your gunner which isnt the right one i would guess. i would do a bit of debug at this point to ensure that the overlap events are being triggered. after your apply damage nodes i would add in a print string and connect the other actor pin to it so you will be able to tell when its fired and by whom. it would also be worth checking the collision responses to ensure that the projectile can overlap the pawns.

avatar image Shalacka Feb 04 '19 at 04:13 AM

What do you mean in regards to the variable in the gunner being wrong, do you mean "EHealth"? it's just a variable I made to store the enemies health

avatar image Shalacka Feb 04 '19 at 04:28 AM

UPDATE: I actually managed to get this working, so when I shoot them I can see their health go down by 10 every shot and then the actor is destroyed and the same applies to me, by any chance is there a similar method of creating health potions, currently I have the same script that I originally showed but instead of losing health you restore health, it works however its supposed to stop giving more health once you hit max hp again (100) in the print string I see values higher than 100 like the example shown.

thank you again so much for the help I really appreciate it.

alt text

alt text

healthpack.png (144.9 kB)
ingameimage.png (626.9 kB)
avatar image ThompsonN13 Feb 04 '19 at 02:34 PM

What do you mean in regards to the variable in the gunner being wrong

if you look at the picture you posted, in the gunner bp you have a Phealth as well as Ehealth. the Phealth doesnt exist in the gunner hence why its grayed, so that leads me to believe its there by mistake.

as for your health pack that is one way to do it but i would go a different route. theres actually two options for this.

first you could create a custom event in your character which is a similar script to your on damage event exept instead of a heath - damage you would have health + heals incoming. then you would also need to add a input to the custom event so you could set the heals from an outside source.

the other option would be to leverage your current damage system. basically have your health pack apply negative damage. say your player is at 50 health and you apply -25 damage, with the current setup it would bring your health up to 75 since 50 - (-25) = 50 + 25. now as for your health going over the 100 max theres a simple solution for that as well, all you need to do is insert a clamp node just prior to setting the health variable. a clamp is a way to set a min and max value a variable can be.

avatar image Shalacka Feb 04 '19 at 05:35 PM

Cool thank you so much for your help I'll look into that and try getting a grasp on it. You've been super-helpful, this is my first time actually posting a question and I can clearly see the advantages in using this if I ever run into problems that other people haven't already asked.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question