# playMaker

### Author Topic: How to make simple enemies  (Read 3314 times)

• Sr. Member
• Posts: 435
##### How to make simple enemies
« on: May 24, 2016, 07:27:48 AM »
I already made a couple of enemies for my game: a fireball and a black hole.
The fireball is generated by a fixed enemy, it travels in a straight line and explodes on impact.
The black hole chooses randomly if to chase the player or wander around, however this enemy is as well very simple: it only checks for 2 collisions and in both cases it destroys the touched things.

Now I need to go further and implement an enemy a bit more complex. It's still very simple, but involves collisions for good, so I'm asking experienced coders here how do they proceed.
I have some ideas in mind, but I would like to know if I'm on the right way.

First of all, I would make a collider below the enemy, to know if it's standing on something or it's falling.
Then I would cast a ray from its eye, with a certain random angle with around 10-15 degrees of spread each fram, so to know what's in front of it.
If I find that an obstacle is nearly touching it, I would turn it 180 degrees.

I can't come with anything easier than this. Any idea?

#### PlaymakerNOOB

• Full Member
• Posts: 219
##### Re: How to make simple enemies
« Reply #1 on: May 24, 2016, 08:48:46 PM »
without knowing what your style of game, its hard to say.

But it seems like its kind of a platformer.  So yes, you can use Raycasting.  Or one of the ways that I did it was to have an enemy have 2 collision boxes.  One directly in front of it to detect walls (Once it "found" a wall, pause and then turn around).  Another box that was in front of it near its feet and basically checking for ground. Whenever it no longer found that it was colliding with ground, it would stop and turn around.

Because I am a lazy, I made a minor change to enable greater flexibility.  I added 1 boolean to each and had them show in the playmaker inspector.  Then saved them as as a template. For the walls one, have a boolean called "TurnAround", if this is false, it continues to go forward despite hitting a wall or hazard.  When TurnAround is true, thats when it turns around when it hits the wall/hazard.  Ditto for the ground tester.  To me, this allowed me recycle my same code and create seemingly different AI types based on the same code.

• Sr. Member
• Posts: 435
##### Re: How to make simple enemies
« Reply #2 on: May 25, 2016, 05:12:37 AM »
Thank you for your answer, I think I am on the right way then!

One question: is there a way to simulate gravity using iTween?
Or should I set up all calculations and movement by the frame?

#### PlaymakerNOOB

• Full Member
• Posts: 219
##### Re: How to make simple enemies
« Reply #3 on: May 25, 2016, 06:09:42 PM »
if my understanding of Unity is correct.. gravity is basically just a vector 3 variable (0.0, -X, 0.0) thats constantly applying AddForce.  X is equal to your scenes gravity.

To me, I try to go with what works.  Then I can go back and refine it if i deem it necessary.

It seems like your attempting to create a blackhole like object, that whenever a player or something gets near it, it pulls them towards the black hole.  I've never really attempted to create a blackhole like object.  But again, since we just talked about boxes, we can use them set as triggers to accomplish the goal.  This is not the most efficient way, but simply the first thing that came to mind.

If we look at the attachment, lets say that square 5 is your black hole.  You can create the collision triggers around it.

Refresher on triggers:
Programming:  Set an FSM and have a global state for trigger enter, to apply ADDForce using a vector 3 variable to whatever it collided with.

box 1 set vector 3 to:   5, -5, 0
box 2 set vector 3 to:   0, -5, 0
box 3 set vector 3 to:   -5, -5, 0
box 4 set vector 3 to:   5, 0, 0
box 5 set vector 3 to:    0,0, 0 (Nothing to apply, as they are where you want them)
box 6 set vector 3 to:   -5, 0, 0
box 7 set vector 3 to:   5, 5, 0
box 8 set vector 3 to:   0, 5, 0
box 9 set vector 3 to:   -5, 5, 0

By doing it this way, you are funneling all objects towards square #5

You could even get rid of the even squares, like 2,4,6,8.  and just make 1,3,7,9 twice as large so the cover the same space that the even numbers one did.

• Sr. Member
• Posts: 435
##### Re: How to make simple enemies
« Reply #4 on: May 26, 2016, 12:20:21 AM »
Uhhh thank you again for the answer, and sorry for making you waste time!
The only thing I need is to apply gravity to my enemy if the player destroys a block under him.
As I can't see anything that applies acceleration over time in iTween, my guess is that I have to pull the function myself, am I right?

#### djaydino

• Hero Member
• Posts: 6098
##### Re: How to make simple enemies
« Reply #5 on: May 26, 2016, 03:48:50 AM »
Hi,
just a little off topic, but iTween is outdated, slow and laggy.

considder using dotween (hotween v2) or hotween
You can get hotween actions here

dotween actions you can find here but i think they are outdated.

you can get updated actions on the asset store here

but they are paid actions

• Sr. Member
• Posts: 435
##### Re: How to make simple enemies
« Reply #6 on: May 26, 2016, 04:32:31 AM »
Thank you Djay, I'm using iTween because it's integrated with PlayMaker I guess... and free.

#### djaydino

• Hero Member
• Posts: 6098
##### Re: How to make simple enemies
« Reply #7 on: May 26, 2016, 06:36:19 PM »
hotween and dotween are free also

except the updated action from dotween not

if you want to go free i would sugest hotween.

• Sr. Member
• Posts: 435
##### Re: How to make simple enemies
« Reply #8 on: May 26, 2016, 07:42:25 PM »
OK I'll try, thank you!
There's some earn in performance?

#### PlaymakerNOOB

• Full Member
• Posts: 219
##### Re: How to make simple enemies
« Reply #9 on: May 26, 2016, 09:04:04 PM »
As Djaydino says, itween is not very good.

Performance wise, I believe DOTween is the best except for like 1 scenario where leantween? i think is better.  There were some videos that showed how great DOTween was when it was released. DOTween claims 400x more performance over HOTween and HOTween was like 5x better than iTween. But at the end of the day, you should be fine with any tween solution instead of iTween.

heres a link to the free and paid DOTween assets.
http://hutonggames.com/playmakerforum/index.php?topic=12849.msg59965#msg59965

Applying gravity once somebody breaks a block below your enemy.  The same concept applies, instead of applying a negative gravity (that pulls the player down) you can apply a positive gravity (that pulls the player to the sky).  Though you would probably want to make it 2x what your gravity is for the scene, so it pulls the player up, rather than canceling each other out.

• Sr. Member
• Posts: 435
##### Re: How to make simple enemies
« Reply #10 on: May 26, 2016, 09:22:14 PM »
Quote
Applying gravity once somebody breaks a block below your enemy.  The same concept applies, instead of applying a negative gravity (that pulls the player down) you can apply a positive gravity (that pulls the player to the sky).  Though you would probably want to make it 2x what your gravity is for the scene, so it pulls the player up, rather than canceling each other out.

Uuuuh what?
I mean, what I want is simply to add gravity to the enemy, because it's not subject to the physics system.
Now, there are two paths: one is to code it by adding a speed variable and increasing it every frame with the gravity variable multiplied with the framerate.
The other path is if iTween has some function for it, because I couldn't find one.
Moving onto DOTween however, so wondering if there is this function as well.

#### djaydino

• Hero Member
• Posts: 6098
##### Re: How to make simple enemies
« Reply #11 on: May 27, 2016, 03:20:18 AM »
Hi,
Maybe instead of simulating the gravity you could use the "set gravity" and/or "use gravity" actions and add a rigidbody to the enemy?

• Sr. Member
• Posts: 435
##### Re: How to make simple enemies
« Reply #12 on: May 27, 2016, 03:31:19 AM »
I am not able to make the enemy movement in physics mode, otherwise I'd do it... that's why I chose the easy way.

#### djaydino

• Hero Member
• Posts: 6098
##### Re: How to make simple enemies
« Reply #13 on: May 27, 2016, 03:28:28 PM »
Hi,
maybe a way you can try is by using set position on you enemy

get hotween on the asset store and its actions here
there should be an action to tween a float and you can use that float on the set position on your enemy to simulate the falling.

Quote
that's why I chose the easy way.

Actually i think your are choosing the hard way tho, but then again i don't know what your project is

• Sr. Member
• Posts: 435
##### Re: How to make simple enemies
« Reply #14 on: May 27, 2016, 09:02:16 PM »
I actually tried to program the enemy using physics, because I'd love to make the whole game physics and because I think that if you set things up right using physics, you have half the work done by the engine.
There are two problems though: moving stuff using physics in a highly controlled manner is more difficult than say to the object "move from here to here while staying vertical", and the other problem is that my game requires enemies to have somewhat a "squared" movement.
To explain better, the game is a puzzle/platform based on cubic blocks. This means that some of the enemies are quite constrained to their path. This first enemy I'm trying to make moves forward, and if it meets a wall or the end of the path, it stops, turns 180 and begin to walk again. With physics, I would have to check that it remains in line, that its orientation is perfectly square, that it doesn't get pushed away.
In a way, making all physics COULD add to the gameplay, and I would have loved it doing it that way, in fact I started to make the enemy physics but stopped immediately due to the problems I thought I'd find. Physics movement should be DONE RIGHT and I don't think I can manage to pull that out at the first shot, and I need to have things done at the moment.
Thanks to cutting physics away, the enemy is already done, adding gravity is trivial now and I'm only asking how to do it because if there's a tween action to do the job I'd prefer to use that for homogeneity, because I used iTween for everything up to now (even though, I'm going to switch to DOTween the next week).

Btw, if you are interested in the project, it's here: http://solomonsring.blogspot.it/