# playMaker

### Author Topic: 2D side-scrolling shooter help!  (Read 10048 times)

#### PalaceAthene

• Playmaker Newbie
• Posts: 13
##### 2D side-scrolling shooter help!
« on: March 14, 2012, 01:56:17 PM »
Hey there, I'm currently making a 2d shooter as a learning experience.

I hope someone can help me with some of my problems.
Currently I have my player character able to move around the screen, but how I keep the player from moving off screen?

#### justifun

• 1.2 Beta
• Full Member
• Posts: 228
##### Re: 2D side-scrolling shooter help!
« Reply #1 on: March 14, 2012, 02:12:18 PM »
You can use a combonation of "get screen height" and get screen width" actions and store those as variables.

and then use "clamp float" to restrict your ships translation positions to never go higher or lower than the screen width/height

you might want to subtract the width/height of the size of your ship (from the maximum screen width/height it can travel) as well, so that it stays visible.
« Last Edit: March 14, 2012, 02:33:17 PM by justifun »

#### PalaceAthene

• Playmaker Newbie
• Posts: 13
##### Re: 2D side-scrolling shooter help!
« Reply #2 on: March 14, 2012, 02:54:21 PM »
I'm not sure how to go about doing that, sorry I'm pretty new to Playmaker.

By 'get screen height' and 'get screen width', do I store them as a Vector3 variable?

Do I use the Vector3 Clamp Magnitude action?

#### justifun

• 1.2 Beta
• Full Member
• Posts: 228
##### Re: 2D side-scrolling shooter help!
« Reply #3 on: March 14, 2012, 03:14:05 PM »
Width and height will both need to be stored as two seperate "float" variables"

if your screen resolution is 1024x768 for example

you'll set up 4 variables

Left (float): 0
Right (float): screen width
Top (float): screen height
bottom (float): 0

I forget if 0/0 is located in the upper left or bottom left of the screen so you might have to switch top to 0 and bottom to screen height

then clamp those 4 variables using float clap, so that your ships position never goes higher than the variables you got form the getting the screen width/height

When i get home an on my machine i can throw together a little example video for you...it'll be a few hours from now though sorry.

#### PalaceAthene

• Playmaker Newbie
• Posts: 13
##### Re: 2D side-scrolling shooter help!
« Reply #4 on: March 14, 2012, 03:36:08 PM »
I see, that makes sense. And I appreciate you making a video for me, and I'm sure other beginners will find the video useful. Thanks again!

#### PalaceAthene

• Playmaker Newbie
• Posts: 13
##### Re: 2D side-scrolling shooter help!
« Reply #5 on: March 15, 2012, 07:24:57 AM »
Been working on the problem of keeping the player from moving offscreen, but still have no solution for it.

Also trying to find a way to make the player fire a projectile, and when it moves offscreen it will destroy itself.

#### justifun

• 1.2 Beta
• Full Member
• Posts: 228
##### Re: 2D side-scrolling shooter help!
« Reply #6 on: March 15, 2012, 08:28:00 AM »
Yeah sorry i wasn't able to make a little tutorial last night for you, ill try again to find some time tonight.

In the mean time....

If you want to make your player fire a projectile.  Here's one way you can go about it.

Create an empty game object and place in in front of your player, in the area where you want your projectile to start from. then parent this game object to your player so that it follows.  name it something like "projectile_start_location"

Now in your player FSM you will want to create a state (ideally on a second FSM on the same player object - this keeps this seperated and easier to debug and visualize) that checks for your fire button using the "Get Button Down" action.

Next create a new state that it will switch to after pressing that button and include the action "Create game object"

For the game object section of "create game object" drag in your bullet prefab.
and for the spawn point, drag in the empty projectile start location object you made earlier "projectile_start_location"

Now each time you press the fire button it will create a new bullet in front of your ship.

Now you need to make the bullet fly through the air.

On the bullet prefab create a new FSM and simply add a "translate" action.

Set its space to "self" and put a value into the "X" value of the action (like 10 or something) - depending on which way your ship is facing, you might have to put -10
and make sure to check the box "every frame".

Make sure this is your "start state" as well.  Now every time you create a new bullet from your player FSM when you press your fire button, you will spawn a new bullet prefab that will immediately start moving 10 units per frame along the X axis.

Now you also want to make the bullet dissapear when it leaves the screen.

For this you can use the handy "is visible" action.... make sure you check the box "every frame" as well so that it checks every frame if your bullet is still visible by the camera.

In the first state you want to create a new event and called it something like "GoToDestroyBullet"
Right click the state and choose "add transition" and then right click the new "...." box and choose "GoToDestroyBullet".

In the "False" drop down of your "is visible" action, choose "GoToDestroyBullet"

Now create a second state in the bullet prefab that will destroy the bullet once its off screen, and simply use the action "Destroy Self"

Now connect the GoToDestroyBullet event from the first state up to the second state that destroys your bullet.

Now everytime your bullet goes offscreen it will automatically be destroyed!

I bet your going to ask how to make the bullet hit something and make it die right?

Here's a few hints to point you in the right direction.

Bullet will need a collider attached to it and set to "Is trigger"
Enemy will need a tag added to it called "enemy" or whatever you want to call it.
Bullet FSM will have a "trigger event" action added that checks every frame to see if its collided with tag "enemy"
Bullet will use "send event to fsm" to talk to the enemy prefab to switch it to a  state on the enemy called "death" which will destroy the enemy and create cool particle effects or whatever.

See if you can figure that next step out first and hit me up for tips again if needs be.

« Last Edit: March 15, 2012, 08:34:01 AM by justifun »

#### PalaceAthene

• Playmaker Newbie
• Posts: 13
##### Re: 2D side-scrolling shooter help!
« Reply #7 on: March 15, 2012, 09:45:59 AM »
Thanks for the help on the bullet, I managed to spawn a projectile, but only one... am I doing something wrong?

Is there a simpler way to translate the player without making it slide around when you depress the movement keys? Like in other Shmups the player ship just moves straight away without any buildup to its max speed?
« Last Edit: March 15, 2012, 11:05:51 AM by PalaceAthene »

#### justifun

• 1.2 Beta
• Full Member
• Posts: 228
##### Re: 2D side-scrolling shooter help!
« Reply #8 on: March 15, 2012, 11:21:34 AM »
Are you sure you are spawning the Prefab of the bullet and not the prefab instance?

You should be dragging in the prefab from the Hierarchy panel of you unity workspace.

Also make sure when you are destroying the bullet you aren't accidentally destroying the starting location game object for example (or else next time you try and spawn another it won't know where to go)

As for your movement question.  If you use "translate" instead of a character controller/controller simple move you can easily get rid of that sliding you are feeling.

#### PalaceAthene

• Playmaker Newbie
• Posts: 13
##### Re: 2D side-scrolling shooter help!
« Reply #9 on: March 15, 2012, 12:15:13 PM »
I figured it out, after the transition from the 'fire' button to the spawn projectile state, I forgot to put a transition back to the 'fire' button state.

As for the sliding movement, I am already using "translate".
« Last Edit: March 15, 2012, 04:46:32 PM by PalaceAthene »

#### Robert Foster

• Playmaker Newbie
• Posts: 36
##### Re: 2D side-scrolling shooter help!
« Reply #10 on: March 15, 2012, 02:57:04 PM »
here down load Get Axis Vector2   this will help

#### PalaceAthene

• Playmaker Newbie
• Posts: 13
##### Re: 2D side-scrolling shooter help!
« Reply #11 on: March 15, 2012, 03:23:17 PM »
here down load Get Axis Vector2   this will help

Do I just drop it onto the player?

#### Mark_T

• 1.2 Beta
• Junior Playmaker
• Posts: 72
##### Re: 2D side-scrolling shooter help!
« Reply #12 on: March 15, 2012, 03:36:51 PM »
I figured it out, after the transition from the 'fire' button to the spawn projectile state, I forgot to put a transition back to the 'fire' button state.

As for the sliding movement, I am already using "translate".

Here is a .rar of the scene.

@PalaceAthene  - Why do I have the feeling that you have included the Playmaker plugin in your project file attachment?

« Last Edit: March 15, 2012, 10:58:47 PM by jeanfabre »

#### justifun

• 1.2 Beta
• Full Member
• Posts: 228
##### Re: 2D side-scrolling shooter help!
« Reply #13 on: March 15, 2012, 03:39:07 PM »
Drop the .cs file attachments form lorddarkness into your "actions" folder in the playmaker subfolder in unity, and you should be able to find it once you re open the actions browser

#### PalaceAthene

• Playmaker Newbie
• Posts: 13
##### Re: 2D side-scrolling shooter help!
« Reply #14 on: March 15, 2012, 04:20:30 PM »
Thanks for all the help! Managed to set player movement and set player firing projectiles. Now its just figuring out how to spawn enemies that move on a set route (e.g. moving horizontally, then diagonally and back to horizontal).

I don't think it will be that hard... maybe.

Already know how to make scrolling backgrounds and landscapes, adding sounds and all that.