Search Unity

Core GameKit! Pooling / Spawning / Combat

Discussion in 'Assets and Asset Store' started by jerotas, Jan 27, 2013.

  1. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Short answer: you can't. For triggered spawners, you cannot get a list of anything that's done being spawned. Once it spawns, the script doesn't keep track of it anymore. There are too many waves types and that would be a huge memory hog to track all that.

    So what you do is you make a subclass of Triggered Spawner that does specific things for your player, and use that instead of TriggeredSpawner on your player. You override the GetSpawnRotation method to look at the player script so it knows what direction the player is facing and return the correct rotation there.

    Now while spawners do not know what they've spawned, Killables do know what they were spawned from. That means that your player ammo knows if it came from Player1 game object or Player2 game object. The spawner that spawned the Killable is kept in the private variable "spawnedFromObject". So if we need to make that a public property that you can make a subclass of Killable and do some logic based on the value of that, we can. Just let us know.
     
  2. Chibwemwn

    Chibwemwn

    Joined:
    Feb 6, 2013
    Posts:
    129

    I think as long as it helps me achieve my goal, then please make it a public property. The thing is, though, I don't understand how to make subclasses. Am I supposed to create a new script, then copy paste the original into the new one? Then make the changes there? I've never made one, before.
     
  3. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Ok it will be a public property in the next version. Code for a simple subclass would be like this, assuming you had a script called PlayerScript with a property "IsFacingRight". Change those to whatever yours are really called.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. public class PlayerTriggeredSpawner : TriggeredSpawner {
    5.     private PlayerScript player;
    6.  
    7.     protected override void SpawnedOrAwake() {
    8.         base.SpawnedOrAwake();
    9.         player = this.GetComponent<PlayerScript>();
    10.     }
    11.  
    12.     protected override Quaternion GetSpawnRotation (Transform prefabToSpawn, int itemSpawnedIndex, TriggeredWaveMetaData wave)
    13. {
    14.         if (PlayerScript.IsFacingRight) {
    15.             return Quaternion.Euler (0, 0, 90);
    16.         } else {
    17.             return Quaternion.Euler (0, 0, 270);
    18.         }
    19.     }
    20. }
    21.  
     
  4. Chibwemwn

    Chibwemwn

    Joined:
    Feb 6, 2013
    Posts:
    129

    Ok, so for some reason this isn't working for me. What should I change?
    Here's what my parameters look like
    :

    Here's my bullet code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Arrow : MonoBehaviour {
    5.  
    6.  
    7.     public Vector2 arrowMaxSpeed = new Vector2(500, 0);
    8.     //private Vector2 arrowMove;
    9.     private playerMovementBasic playerscript;
    10.     private Vector2 speed;
    11.  
    12.     /*void OnSpawned ()
    13.     {
    14.         playerscript = GameObject.FindGameObjectWithTag ("Player1").GetComponent<playerMovementBasic>();
    15.         if(!playerscript.facingRight)
    16.         {
    17.             speed = -arrowMaxSpeed;
    18.         }
    19.         else
    20.         {
    21.             speed = arrowMaxSpeed;
    22.         }
    23.  
    24.     }*/
    25.  
    26.     // Update is called once per frame
    27.     void FixedUpdate ()
    28.     {
    29.         //arrowMove = new Vector2(arrowMaxSpeed.x, arrowMaxSpeed.y * 0);
    30.  
    31.         rigidbody2D.velocity = arrowMaxSpeed;
    32.     }
    33. }
    34.  
     

    Attached Files:

  5. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    I forgot you have that screwy child spawner setup. Get rid of the child spawners. Just have one TriggeredSpawner on the same game object as the playerScript. That one will be the subclass PlayerTriggeredSpawner and not the default Triggered Spawner script. Then you have to set up your code-triggered waves or custom event waves on it again.
     
  6. Chibwemwn

    Chibwemwn

    Joined:
    Feb 6, 2013
    Posts:
    129
    Sweeet, that works now, the rotation is being made. The only problem I have now is that the bullet still travels in the right direction. I'm guessing it has something to do with the vector2D.velocity I'm using. I just need to figure out how to make the bullet move in it's local x,y vector.

    Thank you so much! You've been a big help, as usual.
     
  7. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    I think the easiest way to fix that is to remove the X,Y,Z velocity code from your bullet and just have them move "forward" in every case. They don't need to know about what direction the player is facing. After the rotation is correct (you may have to adjust them), they will "just work right". Although I'm not sure if there is a vector for forward on the 2D stuff. I haven't really used it.
     
  8. Chibwemwn

    Chibwemwn

    Joined:
    Feb 6, 2013
    Posts:
    129
    I got it to work using this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Arrow : MonoBehaviour {
    5.  
    6.  
    7.     //public Vector2 arrowMaxSpeed = new Vector2(500, 0);
    8.  
    9.     private Vector2 arrowMove;
    10.     public float speed;
    11.  
    12.     private playerMovementBasic playerscript;
    13.  
    14.  
    15.  
    16.     void OnSpawned ()
    17.     {
    18.         arrowMove = transform.right;
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void FixedUpdate ()
    23.     {
    24.         //arrowMove = new Vector2(arrowMaxSpeed.x, arrowMaxSpeed.y * 0);
    25.  
    26.         rigidbody2D.velocity = (arrowMove * speed);
    27.         //ridgidbody2D.AddForce(transform.TransformDirection(Vector3.right) * speed);
    28.     }
    29. }
    30.  
     
  9. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Cool, right must be the 2d equivalent of what I was saying.
     
  10. Chibwemwn

    Chibwemwn

    Joined:
    Feb 6, 2013
    Posts:
    129
    Alright, now I've made it so two players can move around and attack each other. The problem I want to fix now, is making sure that the bullets a player spawns don't hurt them. This is the the reason I was using a child spawner (I had a dummy game object with the triggered spawner script attached to it). How do I fix this?
     
  11. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    I believe if you spawn it so it doesn't overlap onto your player, then this won't be a problem, right? You can also override the GetSpawnPosition method inside TriggeredSpawner to modify the position based on the direction the player's facing.

    Normally you don't care much about the overlap and just use the Killable tag filters to make sure the player can't hurt himself with weapons in his layer. But then if both players are in the same layer you wouldn't be able to hurt the other player either. So you could put all players in different layers and make them only able to be hit by other players' weapons as well.
     
  12. Chibwemwn

    Chibwemwn

    Joined:
    Feb 6, 2013
    Posts:
    129
    Ok so this is what I have so far. I don't really know how to structure the position. is it a vector 3.transform?
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. public class PlayerTriggeredSpawner : TriggeredSpawner
    4. {
    5.  
    6.     private playerMovementBasic player;
    7.    
    8.     protected override void SpawnedOrAwake()
    9.     {
    10.         base.SpawnedOrAwake();
    11.         player = this.GetComponent<playerMovementBasic>();
    12.     }
    13.    
    14.     protected override Quaternion GetSpawnRotation (Transform prefabToSpawn, int itemSpawnedIndex, TriggeredWaveMetaData wave)
    15.     {
    16.         if (player.facingRight)
    17.         {
    18.             return Quaternion.Euler (0, 0, 0);
    19.         } else
    20.         {
    21.             return Quaternion.Euler (0, 0, 180);
    22.         }
    23.     }
    24.  
    25.     protected override Vector3 GetSpawnPosition (Vector3 pos, int itemSpawnedIndex, TriggeredWaveMetaData wave)
    26.     {
    27.         if (player.facingRight)
    28.         {
    29.             return  (0, 0, 0);
    30.         } else
    31.         {
    32.             return (0, 0, 180);
    33.         }
    34.  
    35.     }
    36. }
     
  13. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    You're probably going to return this.transform.position + (some offset) for each direction, different offset for each. I doubt it's going to be as large as 180. Probably just a small number.
     
  14. Chibwemwn

    Chibwemwn

    Joined:
    Feb 6, 2013
    Posts:
    129
    So this is what I have so far, but I'm getting an error message.
    Code (CSharp):
    1. protected override Vector3 GetSpawnPosition (Vector3 pos, int itemSpawnedIndex, TriggeredWaveMetaData wave)
    2.     {
    3.         if (player.facingRight)
    4.         {
    5.             return this.transform.position + (1,0,0);
    6.         } else
    7.         {
    8.             return this.transform.position + (-1,0,0);
    9.         }
    10.  
    11.     }
     
  15. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Should be: + new Vector3(1,0,0);
     
  16. Chibwemwn

    Chibwemwn

    Joined:
    Feb 6, 2013
    Posts:
    129
    Okay, so there are no longer any errors, but there's also no offset.

    Code (CSharp):
    1. protected override Vector3 GetSpawnPosition (Vector3 pos, int itemSpawnedIndex, TriggeredWaveMetaData wave)
    2.     {
    3.         if (player.facingRight)
    4.         {
    5.             return this.transform.position + new Vector3(10,0,0);
    6.         } else
    7.         {
    8.             return this.transform.position + new Vector3(-10,0,0);
    9.         }
    10.  
    11.     }
     
  17. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    How do you know for sure there's no offset? Turn off the motion script in the bullet so it won't move at all and fire one both directions. Then look at their positions and compare.

    And/or add a debug statement to the GetSpawnPosition method to print the position to the console.
     
  18. Chibwemwn

    Chibwemwn

    Joined:
    Feb 6, 2013
    Posts:
    129
    Just did that, there is no offset, as I suspected. How do I fix this?
     
  19. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    It looks impossible that the code is not adding the vector if it's in fact running that code. If that code is not running, then that would make sense. If the code doesn't log when you add debug statements inside, then you need to figure out why the code is not running.

    You should have a PlayerTriggeredSpawner script on the player, and no normal TriggeredSpawner and no child spawners.

    Try this code inside your GetSpawnPosition method.
    Code (csharp):
    1.  
    2.  Vector3 offset = Vector3.zero;
    3.  
    4.  if (player.facingRight) {
    5.   offset = Vector3(10,0,0);
    6.   } else {
    7.   offset = new Vector3(-10,0,0);
    8.   }
    9.  var spawnPos = this.transform.position + offset;
    10.  Debug.Log("player pos: " + this.transform.position + " , bullet pos: " + spawnPos);
    11.  return spawnPos;
    12.  
     
  20. Chibwemwn

    Chibwemwn

    Joined:
    Feb 6, 2013
    Posts:
    129
    I've tried that, but I get this message :
    error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected

    I don't know what that means
     
  21. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    There was a typo. Try this:
    Code (csharp):
    1.  
    2.  Vector3 offset = Vector3.zero;
    3.  
    4.  if (true) {
    5.  offset = new Vector3(10,0,0);
    6.  } else {
    7.  offset = new Vector3(-10,0,0);
    8.  }
    9.  var spawnPos = this.transform.position + offset;
    10.  Debug.Log("player pos: " + this.transform.position + " , bullet pos: " + spawnPos);
    11.  
    12.  return spawnPos;
    13.  
     
  22. Chibwemwn

    Chibwemwn

    Joined:
    Feb 6, 2013
    Posts:
    129
    Ok so it works. But the thing is both the first method and this last version work. I was really tired last night and for some reason I didn't bother to increase the x value of the offset. Now I've played around with extreme offsets and it's clearly happening. Thank you for helping so much.
     
  23. Fahrettin

    Fahrettin

    Joined:
    Mar 10, 2014
    Posts:
    81
    ClickToKillOrDamage script doesnt work with 2D collider. Is it normal ?
     
  24. Exeneva

    Exeneva

    Joined:
    Dec 7, 2013
    Posts:
    432
    How long will this remain $10?
     
  25. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    What was the first method again? I think I didn't like it for some reason.
     
  26. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Until you buy it. Just kidding :)

    At the most another 7-9 days.
     
  27. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    I haven't tried it but it should work. First some obvious things:

    1) You must have a Killable script on the object you're trying to kill or damage. Do you?
    2) Turn on "log events" on the Killable and see what the console says when you tap the Killable. Let me know.
    3) You need to have a Rigidbody2d I think on the same game object.
    4) The layer cannot be "ignore raycast" because that script uses a raycast.
     
  28. Fahrettin

    Fahrettin

    Joined:
    Mar 10, 2014
    Posts:
    81
    I tried your advice but it doesnt work. When i turn on "Log events" it says nothing.
    If i put "Box Collider" it works normal.
    "Box Collider" is OK, "Box Collider 2D" isnt.
     
  29. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    You are correct, I just did some searching and learned that RayCasts will not hit 2d colliders. So I'll need to make an alternate version of that script for 2d. You can read more about the problem here:

    http://answers.unity3d.com/questions/596792/raycast-on-a-2d-collider.html

    I'll add the 2d version in the next update.
     
  30. Chibwemwn

    Chibwemwn

    Joined:
    Feb 6, 2013
    Posts:
    129
    Do you mean this?
    Code (CSharp):
    1. protected override Vector3 GetSpawnPosition (Vector3 pos, int itemSpawnedIndex, TriggeredWaveMetaData wave)
     
  31. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    No. Never mind.
     
  32. Fahrettin

    Fahrettin

    Joined:
    Mar 10, 2014
    Posts:
    81
  33. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    No problem!
     
  34. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    I'm trying to figure out a reason that child spawners still need to exist, with custom events having been added. And I'm not coming up with anything really. Also "code-triggered event" 1 & 2 shouldn't be needed either, because custom events can do all that. I may remove both those features at some point in the future unless there are objections?

    Originally, I wrote child spawners so that you could have more than one wave (from multiple child spawners) activate at the same time from a single event. Like in the Triggered Spawner example scene where the mothership launches fighters out of the left and right hangar. But now you could be doing that by firing a custom event and have both of those "child" spawners spawn their waves, but not care if they are child spawners or not, and not have to bother setting up the propogate to children stuff and all that. The only thing that's missing right now is that you have to write one line of code to fire the event. There's no "fire a custom event" action inside a Triggered Spawner event (like enable). Once I add that, I'm failing to see why child spawners need to exist any more. Is there?
     
  35. Fahrettin

    Fahrettin

    Joined:
    Mar 10, 2014
    Posts:
    81
    I have a problem with ClickToKillOrDamage again. If i touch screen and hold my finger(on any position) objects are ignoring touches. Can you make something about that ?
    Solved with Playmaker :)
     
    Last edited: Sep 14, 2014
  36. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
  37. Fahrettin

    Fahrettin

    Joined:
    Mar 10, 2014
    Posts:
    81
  38. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Thank you for your votes guys. Keep 'em coming!
     
  39. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Attention users: I will be traveling for the next 7 days. I will probably have zero internet access, so I will respond to any email / forum questions after that. Sorry it this keeps anyone waiting on answers. If Eric (DT) can answer any questions, he will. Please update to the latest version before reporting bugs to make sure they aren't fixed there.

    Thank you and talk soon!
    -Brian
     
  40. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    I'm back from traveling. The Core GameKit sale is over and we now return to our normal extremely reasonable price of $50. That gives you full pooling, combat and spawner systems.
     
  41. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Core GameKit v3.2.2.9 will be live in 20 minutes. Boss enemies are now possible! New example scene has one. Changelog:

    • Added a button to Pool Boss so you can now click the yellow "X/10 spawned" text to select them all in the Hierarchy.
    • Added a public property "SpawnedFromObject" to Killable so other scripts can read the spawner, if any.
    • Changed confusing icons to actual text buttons (expand all / collapse all / Add).
    • Added DeathDelayStarted method to Killable Listener, so you can play a death animation or other effect.
    • Moved game over music settings into Use Global Waves section where it belongs.
    • Fixed bug: - game over music mode shouldn't do anything when global waves are off. It logged an error when game over is triggered.
    • Added new methods SpawnOutsidePool and SpawnInPool for PoolBoss. This allows you to specify the parent Transform when the object is spawned.
    • Removed support for Pool Manager. SpawnUtility.Spawn and Despawn methods are removed and now PoolBoss.cs is used directly. If you are using Core GameKit's Behavior Designer tasks or Playmaker Custom Actions, you will need to reimport those updated packages to compile again.
    • Added ability to make "Composite Killables". This allows you to, for instance, have a destroyed version of several turrets (or other secondary enemy "parts") on a larger object, like a boss mothership or fortress. Added "Keep Same Parent" checkbox (defaults to true) for Killable Death Prefab to enable this. However, it is a known issue that there is currently no way to respawn the larger object in its original state if one or more of its sub-Killables has changed. Unity does not provide a way to revert prefab instances at runtime. Therefore, you cannot reuse "Composite Killable" objects from the pool if you use this feature and have destroyed or despawned a child. Core GameKit will set the prefab to disabled instead of returning it to the pool when they are destroyed or despawned if they have had any children Killables destroyed or despawned. If they have not, they will be able to respawn and will return to the pool as normal. Make sure to not turn on any despawn checkboxes on the child Killables (like Invisible), because if they despawn before the parent, the parent also will not return to the pool when it dies or despawns. Composite Killables can have multiple levels of sub-Killables.
    • Added new example scene for Composite Killables with a 3-level deep Hierarchy of Killables (a boss enemy) which rotates turrets to fire at you. This can be done with zero coding.
    • Moved Invincibility Settings into its own section in Killable.
    • Added "Invincible while children alive" checkbox, which will make sure a Composite Killable's top-level object cannot take damage while any of its children Killables still exist.
    • Add an option under Invincible While Children Alive to disable colliders on the top-level Killable as well while children alive.
    • Moved Killable and Click To Damage scripts into a new Combat folder instead of Utility folder. They also now appear on the Component -> DarkTonic -> Core GameKit -> Combat menu instead of Utility.
    • Custom event receivers now have an option to "look at" event (the things they spawned already did, this rotates the spawner as well).
    • Added ClickToKillOrDamage2D script for 2d games.
     
  42. dreamlarp

    dreamlarp

    Joined:
    Apr 22, 2011
    Posts:
    854
    Awesome update. You have been busy!!

    This app is one of my favorites as it is but you guys keep making it better. This and the Dialog system I think are two of the best app's on unity.
     
  43. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Thanks dreamlarp! Sorry I don't have the time to do the PlyGame integration. I'm working like a madman as it is.

    If you end up making anything generic enough to share, you can send it to us to be part of the package.
     
  44. dreamlarp

    dreamlarp

    Joined:
    Apr 22, 2011
    Posts:
    854
    Yes this would be next as we are just now working on the Dialog system integration with core game to spawn quest mobs.

    We will start making blocks to work with core game soon to spawn mobs at the players level next. I will add it to our kit and share it with you guys also.
     
  45. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Extremely awesome, dreamlarp!
     
  46. dreamlarp

    dreamlarp

    Joined:
    Apr 22, 2011
    Posts:
    854
    Though we could use video's of thing like "Composite Killables" witch I plan on looking into quite a bit. As I read it I can see some exciting things that we can do with it.
     
  47. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Maybe when I get back stateside. I'm in China for another couple weeks. I did include an example scene with Composite Killable though.

    It's very simple actually. Just create a Killable enemy, and a Killable sub-enemy prefab separately. Then child the sub-enemy under the other one so it's part of the same prefab. Then go change your invincibility settings on the parent to be what you want. Make sure to remove any movement script on the child Killables and any despawn triggers. That's pretty much it! Then repeat by adding more sub-Killables until you have your big enemy.

    I made it as painless as possible and most of it is handled automatically for you.
     
  48. Deleted User

    Deleted User

    Guest

    so..once again I am switching my project back to core..including all my game variables...this where my concern started:

    World Variables - Why Not Boolean?

    Code (csharp):
    1. namespace PreviewLabs
    2. private static readonly string fileName = Application.persistentDataPath + "/PlayerPrefs.txt"
    3.  
    4.  
    5.         public static bool GetBool(string key) {
    6.             if (playerPrefsHashtable.ContainsKey(key)) {
    7.                 return (bool)playerPrefsHashtable[key];
    8.             }
    9.  
    10.             return false;
    11.         }
    12.  
    13.         public static bool GetBool(string key, bool defaultValue) {
    14.             if (playerPrefsHashtable.ContainsKey(key)) {
    15.                 return (bool)playerPrefsHashtable[key];
    16.             } else {
    17.                 playerPrefsHashtable.Add(key, defaultValue);
    18.                 hashTableChanged = true;
    19.                 return defaultValue;
    20.             }
    21.         }
    22.  
    I am already using this solution [PreviewLabs]..why did you not fully extend ?Boolean Player Prefs value??

    I can only assume this was on purpose, and would like to know. It is not a big deal..
     
    Last edited by a moderator: Sep 22, 2014
  49. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Originally I was going to get to it later. Then I thought there was actually no need for it. Is there?
    Maybe there are some checkboxes in the Inspectors that people would like to control with World Variables eh?

    Also we didn't do strings.
     
  50. Deleted User

    Deleted User

    Guest

    well, I actually do not need the world variable component editor, you know, to read/write any datatype that it (PreviewLabs) script supports..so you are right.... no need..I just wanted to double check, in case you reserved it, using it somewhere..I do not know what I thought..just that I am using bools for 'level completed', so I will just continue to script those values..

    before I went "All In" ;-)

    Thanks for the reply

    p-