Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ShadowsFallStudios · Nov 12, 2019 at 11:35 PM · spawningpooling

Help spawning pooled objects with moving spawn point?

I am making a side scrolling endless space shooter where my backgrounds are set up at different depths and my camera and player are set at start to go 7.5f and the first background replaces the last background after the camera goes so far and with the layers i can make a cool parallax effect all of that works fine but i am having issues spawning in my enemys i have an empty gameobject attached to my player and i have that object ahead of me offscreen so that i can use that for my spawn position x and then I fill in the y value with a random.range with the min screen height and the max screen height and everything works... well for the first pool spawn but everytime they are respawned after that they keep spawning further and further ahead of the spawn point to where they are too far from the screen to ever let me catch up so they just keep activating and deactivating any help would be greatly appreciated this has been driving me crazy all night i just cant seem to figure out whats going on

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SkyriftStudios · Nov 13, 2019 at 12:17 AM

  1. Is your pooled objects connected to your parrallax or any other objects because if your player or your object parent is changing size then your pool objects will too. Make sure that the parent is set to either null or an object that has no changes in transform, position, rotation, scale, etc. You can make a script to make the parent follow the player without directly being attached.

  2. Can you provide images of your unity editor and the problem? Maybe a snippet of code for your pooling object system? Just so I can have a better understanding of this problem.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadowsFallStudios · Nov 13, 2019 at 01:31 AM 0
Share

I fixed the spawning issue by taking the velocity off of the drone but now the drones come across the screen too fast and any way that I try to add velocity to them it seems to grow exponentially on every respawn any ideas on how to set my velocity just once ?

avatar image
0

Answer by ShadowsFallStudios · Nov 13, 2019 at 12:28 AM

I have only been learning to code for 2 months so be easy on me lol This is my first project where I am doing everything on my own. I think the issue may be involved with the drone ai script where i set the speed to 5f and i think it might be adding to that speed everytime it reactivates thank you for your reply and any help you can give.

OBJECT POOLER Script

public class ObjectPooler : MonoBehaviour { [System.Serializable] public class Pool { public string tag; public GameObject prefab; public int size; }

 public static ObjectPooler Instance;

 private void Awake()
 {
     Instance = this;
 }

 public List<Pool> pools;
 public Dictionary<string, Queue<GameObject>> PoolDictionary;
 private float randomY;

 private void Start()
 {
     PoolDictionary = new Dictionary<string, Queue<GameObject>>();

     foreach(Pool pool in pools)
     {
         Queue<GameObject> objectPool = new Queue<GameObject>();
         for (int i = 0; i < pool.size; i++)
         {
             GameObject obj = Instantiate(pool.prefab);
             obj.SetActive(false);
             objectPool.Enqueue(obj);
         }
         PoolDictionary.Add(pool.tag, objectPool);
     }
 }

 private void Update()
 {
     randomY = Random.Range(-7f, 11f);        
 }

 public GameObject SpawnFromPool(string tag, Vector3 position, Quaternion rotation)
 {
     position = new Vector3(transform.position.x, randomY, transform.position.z);
     rotation = Quaternion.Euler(0f, -90f, 0f);

     if (!PoolDictionary.ContainsKey(tag))
     {
         Debug.LogWarning("Pool with tag " + tag + " doesn't exist.");
         return null;
     }

     GameObject objectToSpawn = PoolDictionary[tag].Dequeue();
     objectToSpawn.SetActive(true);
     objectToSpawn.transform.position = position;
     objectToSpawn.transform.rotation = rotation;
     IPooledObject pooledObj = objectToSpawn.GetComponent<IPooledObject>();

     if(pooledObj != null)
     {
         pooledObj.OnObjectSpawn();
     }

     PoolDictionary[tag].Enqueue(objectToSpawn);
     return objectToSpawn;
 }

interface script

public interface IPooledObject { void OnObjectSpawn(); }

droneSpawner Script

public class DroneSpawner : MonoBehaviour { private Vector3 spawnLocation; private float randomY; private ObjectPooler objectPooler; private bool isSpawning;

 private void Start()
 {
     objectPooler = ObjectPooler.Instance;
 }

 private void Update()
 {
     Debug.Log(spawnLocation);
     spawnLocation = new Vector3(transform.position.x, randomY, transform.position.z);
     randomY = UnityEngine.Random.Range(-7f, 11f);
     StartCoroutine(SpawnDronesCo());
 }

 private IEnumerator SpawnDronesCo()
 {
     if (!isSpawning)
     {
         isSpawning = true;
         yield return new WaitForSeconds(UnityEngine.Random.Range(2f, 6f));
         objectPooler.SpawnFromPool("Drone", spawnLocation, Quaternion.Euler(0f, -90f, 0f));
         isSpawning = false;
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

117 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

custom spawn unet weird behaviour ? 0 Answers

Pooled object keeps changing spawn position? 2 Answers

Will Instantiating prefab with many components slow my game? 1 Answer

Get components of multiple instantiated objects? 2 Answers

How Increase And Decrease And Reset Spawn Rate Over 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges