playMaker

Author Topic: The Final Challenge! Arrays and Prefabs.  (Read 832 times)

curb47

  • Sr. Member
  • ****
  • Posts: 256
The Final Challenge! Arrays and Prefabs.
« on: October 02, 2020, 09:24:16 AM »
Hello there,

So, after many many months of getting up at 4am and plenty of help from djaydino and the Playmaker forum, I'm finally reaching the finishing line with the core gameplay mechanics of my spaceship game.

I have one final challenge before the gameplay loop is complete, and then I can move on and focus on refining visuals in Shader Graph & VFX Graph.

I've been putting this final challenge off, because I looked into it before and it kinda fried my brain... actually, 99% of what I've done has fried my brain and if it wasn't for the help I've received here I'd have given up a long time ago.

The final challenge

In my game, you are tasked with harvesting 'shiny stuff' (crystals, gold, fairy dust, whatever) from 'sentient beings' that float around near the surface of the planet. When you find these sentient beings you start shooting them and they accelerate away from you at high speed. You chase them, guns blazing until they explode and release a load of the Shiny Stuff that you fly through to collect, and take back to the Mother Ship for your evil corporate masters.

The challenge is that I want to instantly spawn a load of Shiny Stuff (prefabs, pooler spawn?), but the amount of Shiny Stuff varies depending on a simple variable; the altitude at which you destroyed the Sentient Being.

Basically, the closer you are to the planet's surface as you're whizzing along, the higher the reward because there are more obstacles and hazards that can destroy you. If you destroy the Sentient Being at high altitude, it's much easier so the reward should be lower.

I'm thinking the way forward with this, is to load the prefab of the Shiny Stuff into an array of say, 10 objects, and then when the Sentient Being is destroyed I somehow pooler spawn from the array, the corresponding number of Shiny Stuff. Let's say, it spawns all 10 if you're really close to the ground, and it only spawns 1 or 2 if you're high up.

I've really tried to solve this on my own, but I can't get it. I've watched djaydino's Jinxter Games Youtube videos, which are great, but I can't make the connection between what the videos are showing and how to implement the techniques in my game.

It got me thinking; how do you spawn a pooled prefab into an array, but not the scene, and then that array is used to spawn the prefabs into the scene?

I set up an array variable, size 10, array type Game Object and dragged the same prefab (Shiny Stuff) into each of the 10 slots in the array variable, but what on earth do I do with it?

To be honest, the Array maker framework looks awesome, and I can understand the power of using arrays, but there are so many Playmaker actions for it, I simply don't know where to begin.

djaydino, if you're reading this, please please can you help me?

Thank you. Again.

J.



Gav (HeyBud)

  • Full Member
  • ***
  • Posts: 126
    • Tumblr
Re: The Final Challenge! Arrays and Prefabs.
« Reply #1 on: October 02, 2020, 09:19:30 PM »
You could have a game object prefab with children of different 'shiny stuff' (crystals, gold, fairy dust, whatever) rather than an array. Have different packs for each distance from the surface. Like ShinyPack_Closest, ShinyPack_MidRange, etc. and just spawn those on killing sentient beings.

You could use a "raycast" action to determine the distance from the surface, then when bad guy is killed, have a "float switch" for various ranges that determines which ShinyPackPrefab is appropriate to create at the kill site. (You can have nested prefabs in unity which is nice so all the children of each pack can be individual prefabs inside an empty object like a folder holding them.)

if you really need to use an array, just use "array get next" and "create object" to add it to the scene then do a little loop of states until they're all in the scene from the array. "array get random" is also handy. Anyway you could use the raycast distance the same way as above with a "float switch" event, and  have it lead to different state loops. (if you don't want to loop you could load the array with game object clusters like I said in the first paragraph.) By the way to make a loop: have an int named Loop and "int add" each time it gets and spawns from the array. you'll have to use "int compare" after each loop until the desired number of sparkly drops is reached. if it hasn't been reached have it loop back to the "array get" action. Hope that doesn't sound too confusing.

I hope I understood correctly. Let me know.  The first way is how I'd handle it.
« Last Edit: October 03, 2020, 12:40:14 AM by Gav (HeyBud) »

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: The Final Challenge! Arrays and Prefabs.
« Reply #2 on: October 03, 2020, 01:33:06 AM »
Hi Gav,

Thank you for your advice, your first suggestion makes complete sense to me.

I don't need to use an array, in fact, I feel like I've missed something because I haven't used a single array in the entire game, the complexity has kinda scared me off a bit. I'm sure that if I gave my project to djaydino, or yourself, it would come back with a bunch of stuff changed to an array system.

So, yes, I could have 3 different prefab objects, Shiny_Pack_Top, Shiny_Pack_Middle and Shiny_Pack_Bottom. In each of these prefabs I have 2, 5 and 10 prefabs of my single Shiny Stuff crystal/star thingy.

I could then make a Pooler Pool for each of these Shiny Packs, and Pooler Spawn the appropriate one based on altitude of Sentient Being death.

One thing you wrote that got my attention is - 'You can have nested prefabs'. This is just a term for what you described, right? A nested prefab would be the 10, 5, and 2 prefabs inside the Game Object, which is also a prefab?

Another thing... You suggest using raycast to get the distance from the surface. This is interesting because I would instinctively use Get Position and use the Y axis for a float compare. Is there an advantage to using raycast over Get Position?

I think your suggestion of using the 3 different nested prefabs works for me. One of the other benefits would be that it would instantly spawn the Shiny Stuff in one cluster; the array system would spawn them rapidly one after the other, right?

I did actually explore using the particle system to create the shiny stuff. In my day job I do a lot of work in Cinema4D and After Effects, and in Cinema4D for example, the particle system allows you to assign an object as a particle, which is great because it uses the physics of the particle system but the particle object is still treated like a regular object, with rigid body, collider tags etc. It doesn't seem that this is possible in Unity.

In fact, that's the best way to visualize the kind of effect I'm going for with my Shiny Stuff... a particle explosion, with inherited velocity and air resistance etc.

Okay then.

Thanks for your help Gav, I really appreciate it. I'll try out the nested prefabs idea and get back to you. I won't be touching Unity this weekend as I need a break, but Monday I'll get on it.

Have a great weekend!

J.

« Last Edit: October 03, 2020, 01:59:08 AM by curb47 »

Gav (HeyBud)

  • Full Member
  • ***
  • Posts: 126
    • Tumblr
Re: The Final Challenge! Arrays and Prefabs.
« Reply #3 on: October 04, 2020, 03:12:09 PM »
Hey J,


Arrays are great for storing groups of stuff. I was just offering a simple solution.
e.g. If you wanted to randomize the shiny stuff more you could use an fsm that gets the distance and grabs a random shiny pack from a Top_Distance_Array that would have a game object list of Shiny_Pack_Top_1, Shiny_Pack_Top_2, Shiny_Pack_Top_3. Still using the nested prefabs, but a few variation of children.  You might not need to worry about pooling with this small amount unless you have obvious dips in performance. That's just my opinion though :)

Raycast would work well if you have obstacles on the ground like hills or buildings and you want to test distance from the peak of those ground obstacles under the player ship.  Checking Y position is fine if you have a flat ground. Raycast 2d action is easy to implement and has some good options for testing. I used it for a character controller where I wanted to know how close to the ground they were so I could cheat the jump if they were close enough to landing. I got the idea from Celest developers.

Is your project 2d? Maybe not since you said your background is in C4D.  If not disregard this last bit and use Add Explosion Force action instead.

Anyway you mentioned wanting the packs to act like particles. I know you can have unity particles be affected by gravity but i don't think you can attach an fsm to them so you can pick them up. Someone correct me. What you can do to add a little juice is have an effector 2d in your pack. Add force to your pickups as they are spawned so they feel like particles. Here's a good tutorial. (You can also add a random rotation state when the fsm starts to make it feel more randomized.)
That's cool djaydino has helped you, he's helped me a bunch in my games too.  Your project sounds fun!
-Best of luck, Gav
« Last Edit: October 04, 2020, 03:27:45 PM by Gav (HeyBud) »

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: The Final Challenge! Arrays and Prefabs.
« Reply #4 on: October 06, 2020, 03:35:03 AM »
Hi again Gav,

Yes, my game is 2.5D, so using the 3D framework, but the action happens on a flat plane. I've got 3 cameras, for layer culling; one Perspective camera for background objects so parallax is maintained, one Orthographic camera for the gameplay, and another Perspective camera for foreground objects, for parallax again.

It's quite a nice little setup, giving the gameplay a nice retro 2D vibe, whilst embedded in a full parallax 3D environment, so the 3D models of asteroids floating around and other spaceships keep their 3D integrity.

All my objects are 3D, apart from scenic objects (scrolling landscapes etc) that are either sprites or quads.

So...

I've been trying to set up Explosion / Add Explosion Force to scatter the Shiny Stuff upon spawn, but I can't get it working. I've opted for Explosion, and not Add Explosion Force as it seems more suited for multiple objects within a radius of the Explosion.

I've been working on prefab ShinyStuff10, which as the name suggests, has 10 prefabs of Shiny Stuff as Children. All 10 are a duplication of the one master ShinyStuff prefab. They all have the rigidbody component, and a collider.



On the ShinyStuff10 there is one simple FSM, that simply detaches children, gets position of itself, and using that position variable as the center of the Explosion.



I thought that this arrangement would work, but alas, it doesn't seem to do much. Maybe it's got something to do with Tags and/or Layers? I can see on the Explosion action there are settings for Layers, and I've experimented with it, but again, no joy.

Here's a screen grab of the resulting spawn from the current setup. The axis marks the point of the ShinyStuff10 game object, which was the parent of the ShinyStuff.



tl;dr? How on earth do you get the Explosion action working?