playMaker

Author Topic: Raycast does damage to INDIVIDUAL enemies...how?  (Read 4135 times)

JamieDKelly

  • Playmaker Newbie
  • *
  • Posts: 4
Raycast does damage to INDIVIDUAL enemies...how?
« on: November 24, 2016, 11:48:23 PM »
There are a few flavors of this problem online and in these forums, but they always end up at dead ends or the OP just started using colliders. I want to use a raycast to shoot a laser and hit an enemy. This works just fine. However, when the laser hits an object tagged with Enemy, I want it to deduct health from JUST that enemy.

Now normally, I would simply use a non-global variable which exists only within the context of that enemy. I would say, "when the raycast hits, deduct x value from the float of this variable". The issue is that I can not, for the life of me, figure out how to send a message to JUST that enemy and to have JUST their health deducted, rather than a specific item that all enemy prefabs share, thus killing all enemies at once when one dies.

All of my problems would be solved if the collision event command accounted for raycast collisions, but since it only works for projectile collisions, it doesn't seem to detect that anything has happened when just a raycast hits it. There seems to be no equivalent for raycasts.

I store the impact point AND the object hit as variables, but I don't know how to manipulate the health of the object hit.

What would be the best way to approach this?

Thanks!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Raycast does damage to INDIVIDUAL enemies...how?
« Reply #1 on: November 25, 2016, 01:16:27 AM »
Hi,

  I think you are confused by the term "global event", it's not because an event is global that you must broadcast it to all, simply use a sendEvent action targeting the object your raycast has hit, and use a global event for it, only that target will receive it.

Have you tried that already? can you get a fsmGameObject variable of the hit target of your raycast? if yes, then you have everything already in place.

Bye,

 Jean

JamieDKelly

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Raycast does damage to INDIVIDUAL enemies...how?
« Reply #2 on: November 25, 2016, 04:25:29 PM »
First off, thanks so much for your diligent monitoring of these boards. It's a great relief to see you offering so much support to all of our questions.

I do get the object that was hit and store it in a variable, HitObject, if that is what you mean.

Using Send Event was one of the first things I tried. What I can't wrap my mind around is how I tell Playmaker to send the event to the SPECIFIC game object that I have hit. I can tell it to always find an object in the scene, but if I have enemy 37 which was created from a prefab via a spawner, I don't understand how it knows to send the message to that one spawned instance of the enemy in particular.

Now, I originally assumed that the stored variable HitObject would tell it what it just hit, and it would send whatever message I wanted to send to that. However, I can't choose that as an option in Send Event. If the event target in Send Event could be a variable (HitObject), I feel like it would work, but I only get a drop down menu. This may not be the proper route for other reasons, so let's just stick with getting Send Event to work.

I have done some additional tests. I have an FSM on the enemy prefab. It has a state "took damage". I have an impact manager game object as well which tells my game what to instantiate when it hits different things (sparks, blood, etc.) based on tag. I have told it to instantiate a blood effect when it hits something tagged enemy. This works fine. I have added an action to this, the Send Event action. I'm trying to say, "after drawing the blood, send an event to the Enemy Health Manager on the enemy that I just hit". Do this by sending the event, "took damage" in that specific FSM (the spawned enemy prefab).

This all works perfectly if I tell my Send Message action to target a Health Manager which exists in the scene already, since it knows exactly what to affect.

It stops working at all when I tell my Send Message action to target a Health Manager which exists in the prefab.

The problem appears to be that Playmaker doesn't know which specific FSM to send it to unless it's already in the scene and I point straight at it, which I can't do if I am spawning prefabs.

Hopefully this makes sense. Bold was added for clarification.

Thanks!
« Last Edit: November 25, 2016, 04:37:07 PM by JamieDKelly »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Raycast does damage to INDIVIDUAL enemies...how?
« Reply #3 on: November 28, 2016, 01:50:51 AM »
Hi,

 Prefabs sitting in the project browser are not part of the scene, and are not "Responsing" to anything. Only Instances of these prefabs are in the scene and they work as regular gameobjects in the scene Hierarchy.

 It may be obvious, but I just wanted to make sure you got that, from your description, it seems you are getting confused with this.

So your enemy manager prefab HAS to be in the scene hierarchy, and the sendEvent has to target it from the hierarchy, not the prefab source itself.

Can you confirm the above?

When you are spawning prefabs, do you mean programmatically or by drag and dropping from the project browser to the scene hierarchy?

-- if programmatically, you can always get the pointer to the instance you created from a prefab, all PlayMaker actions that instantiate a prefab do have this option.

-- if manually, then you have plenty of options:

-- simply have a global variable that point to your prefab instance
- When your manager on start or at some point, broadcast an event "I AM THE MANAGER" or something, every fsm interested in this should then implementing this global event and store in a fsmvariable the sender of this event
- or use "FingGameObject" actions, prefer using pur tag search and have a dedicated tag for each manager, it's better than searching name by string ( a Unity performance issue).

 Does that help you getting a better picture?

Bye,

 Jean

JamieDKelly

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Raycast does damage to INDIVIDUAL enemies...how?
« Reply #4 on: November 30, 2016, 03:14:37 PM »
I completely understand what you mean by prefabs in the project browser vs. prefabs in the scene. This is the core of my problem as I don't want every enemy that will exist throughout the duration of the level to exist IN the scene, I just want a spawner which spawns a prefab of that enemy at certain times.

I am spawning prefabs programmatically. I have a gameobject called "Enemy Spawner" in the scene which creates an enemy at a certain point at a certain time interval while telling the UI how many enemies have spawned in total. It is simply spawning the enemy prefab into the scene from a prefab which exists in the project.

Even if I did it manually, and I send the event of getting hit, it will simply send the event to every enemy in the scene and they all die at the same time.

If I was to start over, what would be the best way to spawn enemies at certain time intervals at certain locations while being able to damage each enemy individually? How is it normally done?

Thanks!
« Last Edit: November 30, 2016, 03:16:30 PM by JamieDKelly »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Raycast does damage to INDIVIDUAL enemies...how?
« Reply #5 on: December 01, 2016, 03:58:33 AM »
Hi,

yes, I think there is something you are missing, but I am not sure what it is actually :) let me try to reword.

 Each prefab you instanciate in your scene have it's own logic implemented, and so you can have ten instances of the same prefab in your scene, you can still send a global event to only one of them, providing you have a reference to that instance, AND that in your send event you target this instance, and not broadcast.

Does that help a bit or you understand that part as well? it doesn't matter when in the scene if this is a regular object or a prefab instance, it's treated exactly the same, so when you hit one of this prefab instance, you get the reference of that instance being hit, and that's what you use as a target to send a message, and NO other object will receive it even if they implement also this global event.

If you keep struggle, I'll do a sample to show you.

Bye,

 Jean

JamieDKelly

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Raycast does damage to INDIVIDUAL enemies...how?
« Reply #6 on: December 04, 2016, 10:36:04 PM »
And that all makes sense, but I still don't know HOW to send the event to just one or how to have a reference to each individual object. An example would be excellent!

mdotstrange

  • Hero Member
  • *****
  • Posts: 555
    • Can't code? Who cares! Make games anyway!
Re: Raycast does damage to INDIVIDUAL enemies...how?
« Reply #7 on: December 04, 2016, 11:05:05 PM »
You use the cast to get the "hit object" then you can either send an event like "Damaged" to that object- you can also send data with the event like how much damage to do as well as a float/int etc
Indie game dev and instructor at the Strange School. Learn Playmaker at the Strange School!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Raycast does damage to INDIVIDUAL enemies...how?
« Reply #8 on: December 05, 2016, 03:03:02 AM »
Hi,

 I upgraded an old Unity 3 sample and tweaked it to demonstrate this.


https://twitter.com/JeanAtPlayMaker/status/805682031455768576

so check out how from a given gameobject target, you can then send an event to that target only, in this case, it will scale down and destroy self.

not as well, that I positionned the global transition as a transition of the start state, so that it only receive that event when in that start state ( else if you keep  firing at the target as it scales down it would restart the scale down anime).

 Let me know if you have questions

Bye,

 Jean