playMaker

Author Topic: Add Force to specific enemy types?  (Read 1801 times)

wirejaw

  • Playmaker Newbie
  • *
  • Posts: 49
Add Force to specific enemy types?
« on: November 18, 2020, 11:56:43 AM »
I'm working on a 2.5d platformer and have setup my player character to punch enemies. The desired outcome is that the player presses an attack button to punch an enemy at close range, and then the enemy is launched away (in the direction the player is facing).  So far I've got the basic system in place using a Raycast, and on the Hit Event I have it set to Add Force to my enemy object.

This is working but the problem is that it only works on the one enemy object, and I want to set it up so that it works whenever I punch any enemy object that can be "punched" but not enemies that can't be punched.  I've investigated potentially using tags or layers but no luck.

Any ideas?  Thanks for any help.
« Last Edit: November 18, 2020, 04:24:26 PM by wirejaw »

LuminRabbit

  • Full Member
  • ***
  • Posts: 161
  • Lumin Rabbit
Re: Add Force to specific enemy types?
« Reply #1 on: November 18, 2020, 05:40:24 PM »
I don't know much about it haven't set it up but Im not sure why it would only work on one enemy..

How about setting it to Impulse instead of force (so that there's new impulse every time, I believe if I use force once its gone but if I use impulse it can be repeated)?

« Last Edit: November 18, 2020, 06:12:16 PM by Kodagames »
Have I said how much I love playmaker!!! This is one amazing tool

wirejaw

  • Playmaker Newbie
  • *
  • Posts: 49
Re: Add Force to specific enemy types?
« Reply #2 on: November 18, 2020, 06:12:55 PM »
Thanks for the reply Kodagames.  What's confusing me (and apologies as I'm new to Playmaker) is trying to figure out how to arrange it so that Add Force is applied to any enemy that is punched, not just the one enemy I have specified in Game Object.

In my current setup, it seems I am restricted to using only one enemy game object (in Add Force - Game Object), and when I punch that specific object, Add Force works fine and that enemy is launched away.  However, I have several enemies and I need to figure out how to use Add Force so that when any of them are punched, they react the same way.

Also, I'm casting a ray from my player and triggering the Add Force as the Ray Cast Hit Event, if that helps at all.

Maybe I'm missing something really basic, or going about this the wrong way, but if there is a way to use the same Add Force state for all enemies it would be ideal.

wirejaw

  • Playmaker Newbie
  • *
  • Posts: 49
Re: Add Force to specific enemy types?
« Reply #3 on: November 18, 2020, 08:32:08 PM »
I got it working:

-In the "Store Hit Object" of the Raycast, I created a new variable (I called it "EnemyHasBeenHit")
-In the Add Force state I specified this variable as the Game Object.

Learning is good.

wirejaw

  • Playmaker Newbie
  • *
  • Posts: 49
Re: Add Force to specific enemy types?
« Reply #4 on: December 11, 2020, 04:57:21 PM »
Revisiting this I am having another issue.  I need to set it up so that when Add Force is applied to the punched enemy object, it is pushed in the direction the player is facing (the direction the raycast is pointing).  In Add Force I can set it so that the enemy object is pushed in a direction (X, Y, or Z) but not sure how to make it push in the direction of the raycast.  I tried messing with Vector in Add Force but not having any luck. 

Any tips or help is appreciated!  Thanks.

LuminRabbit

  • Full Member
  • ***
  • Posts: 161
  • Lumin Rabbit
Re: Add Force to specific enemy types?
« Reply #5 on: December 12, 2020, 08:20:14 PM »
If the Raycast is facing left you may be able to Add Force in a negative direction if facing right Add force in the positive direction (if your force is 25, left could be 25 multiplied by -1, and if facing right just leave the variable at 25).

Also from previously in this post I'm not sure if something like this would work for you but I did a little test. I don't like to use raycasting because its expensive but then again probably not these days lol

Because your game is 2.5D Im not sure how directions will work?

I created a player (basic cube) with 2 child objects (a sphere and a empty object named distance, more on these in a minute). Add an FSM to the player gameObject:

Start State:
Get Button Down (Send Event "Punch"),
Get Axis "Horizontal", Store "xPos", check Every Frame
Translate X "xPos" (this is just for testing).

Punch State:
Move Object, Object To Move "Sphere", Destination "Distance" (you can move this object forward in the X to where you want the end point of your punch), Finished Event "Finished"

Next State:
Wait: Time "0.1", Finished Event "Finished"

Next State:
Wait: Time "1", Move Object, Object To Move "Sphere", Destination Player, Finished Event Finished.

Thats it for the Player (it has a Rigidbody, Box Collider nothing fancy). So when you hit the Fire 1 button the cube throws a punch.

The last GameObject is the Enemy:
Basic Cube, with a Box Collider and Rigidbody "I used constraints to Freeze Position Z, Freeze Rotation Y, Z" in the inspector of the rigidbody. Ok add an FSM to the Enemy:

Start State:
Collision Event, Collider Tag "Sphere" (also add a "Sphere" tag on the sphere), Send Event "Hit" (You can store the collider and force if you like?), Get Collision Info, Contact Point "contactPoint" variable

Next State:
Add Force, At Position "contactPoint", Vector X "5", Space "World", Force Mode "Impulse", Add a Finished State that goes back to the start state.

And that's it hope you under stand all of this (after each comma I put the action you will need). The cube can move left and right with the arrow Keys or A & D, if you click the Left Mouse button the player shoots a sphere that goes out to the distance of the Distance object, then returns in 0.1 seconds (you can turn off the Mesh Renderer setting to hide this. Each time you are close enough to hit the Enemy it gets knocked back.

Im sure there are more ways to do this and this may or may not work for your situation but its an idea (you can quickly try in a new empty scene with a cube as the ground) as another option.
 

« Last Edit: December 12, 2020, 08:40:08 PM by Kodagames »
Have I said how much I love playmaker!!! This is one amazing tool

wirejaw

  • Playmaker Newbie
  • *
  • Posts: 49
Re: Add Force to specific enemy types?
« Reply #6 on: December 14, 2020, 12:32:20 PM »
Thank you Kodagames for all of this info!  I've gone through your instructions step by step and have things close to working, but I'm having an issue trying to get the punched enemy to be launched in the direction it was hit (X direction left or right depending on player facing direction).

In the Add Force state on the enemy: 

Add Force, At Position "contactPoint", Vector X "5", Space "World", Force Mode "Impulse", Add a Finished State that goes back to the start state.

...I'm confused by "Vector X "5."  My assumtion is that I need to fill in the fields like this:

Add Force
Game Object - Use Owner (my enemy prefab)
At Position - ContactPoint (this seems to be working for me)
Vector - ? (is this used..?)
X - 5 (this works, but only pushes the enemy in positive X direction, to the right for my game)
Y - None
Z - None
Space - World
Force Mode - Impulse (I've also tried Force which works too)
Every Frame - Unchecked

So with the above settings, combined with all of the other previous steps for the player, sphere, etc., it works but the enemy is punched in positive X direction only, regardless of which direction my player is facing.

Is there something I'm missing?  Thanks again for all of your help!

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: Add Force to specific enemy types?
« Reply #7 on: December 15, 2020, 06:02:07 AM »
You could set up a simple float variable which switches to plus/minus when your character changes direction.

So, for example, if you're using Z and X on the keyboard for left / right movement,  include a Set Float action somewhere in your logic.

Off the top of my head (I'm Christmas shopping at the moment... well, my wife is, I'm standing around getting bored)... Get Key Down Z > Set Float  punchForce = -20

Get Key Down X > Set Float punchForce = 20

Then, when you're applying force for the punch, use the punchForce variable in the X value.

You will probably have your player direction controls on a different FSM, so on the Punch FSM that has the Apply Force action, you can get the punchForce value by using Get FSM Float.

Basically,  set a punchForce float variable to either minus/positive when your direction changes and grab that value using Get FSM Float to use as your force.

Setting a float variable for punchForce is also handy because you can set it to different values if you have power-ups, for example.
« Last Edit: December 15, 2020, 06:04:32 AM by curb47 »

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: Add Force to specific enemy types?
« Reply #8 on: December 15, 2020, 06:13:27 AM »
Actually,  you could just set your punchForce float once, and use Float Multiply to turn it to a negative value for when character is facing left.

That way you can define the power of the punch one time only; which is helpful for keeping track of your values.

Eg:

punchForce = 20 (or whatever value is good for you)

Get Key Z > Float Multiply punchForce * -1

Know what I mean?


wirejaw

  • Playmaker Newbie
  • *
  • Posts: 49
Re: Add Force to specific enemy types?
« Reply #9 on: December 17, 2020, 11:35:50 AM »
Cool thank you curb47!  I will try this out today and let you know if I run into issues.   

LuminRabbit

  • Full Member
  • ***
  • Posts: 161
  • Lumin Rabbit
Re: Add Force to specific enemy types?
« Reply #10 on: December 17, 2020, 07:37:19 PM »
wireJaw,

I've been away for a few days, but to answer your questions:
In the Add Force action I had Vector set to none, and you have the rest setup as I did.

curb47, hit the nail on the head by using float multiply to turn your positive value into a negative value and vice versa.

One more thing that may help with collisions when using this method is to drag and drop the Sphere Collider (into your punch state) then select Set Property and choose enabled (set value to true (check the box), then after the wait state when you move the sphere back do the same thing but set the value to false (uncheck the box) this way the Sphere doesn't interact with your player.

Glad you are getting closer (I'll be studying up on hit boxes etc., as I find it interesting).
Have I said how much I love playmaker!!! This is one amazing tool

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: Add Force to specific enemy types?
« Reply #11 on: December 21, 2020, 02:27:48 AM »
Hey Kodagames,

Hope you're well.

I kinda stole your thunder a bit, because you pretty much said the same thing a few posts before...

"...If the Raycast is facing left you may be able to Add Force in a negative direction if facing right Add force in the positive direction (if your force is 25, left could be 25 multiplied by -1, and if facing right just leave the variable at 25)."

Sorry.

J.

LuminRabbit

  • Full Member
  • ***
  • Posts: 161
  • Lumin Rabbit
Re: Add Force to specific enemy types?
« Reply #12 on: December 23, 2020, 05:28:01 PM »
Curb47,

Hahaha no worries, we’re all here to help and glad to see you trying to help others, I frequent the forums often with questions as I learn this amazing tool without playmaker I don’t know if I could create games, anyway hats off to you my friend, we can do amazing things by helping each other!
Have I said how much I love playmaker!!! This is one amazing tool