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 DatTardisDoh · Apr 30, 2018 at 09:43 PM · instantiatespawnif-statementsifrunner

Spawning issues

I have a project that is essentially just an endless runner. I just recently have coded in the part that makes it so I cannot have two blocks spawn adjacent to each other that will block the path to the player.

I do so by creating prefabs for all 19 possible platforms: (0 denotes clear, x denotes pitfall and b denotes block)

 platform1: 000
 platform 2: b00
 platform 3: bx0
 platform 4: bb0
 platform 5: b0x
 platform 6: b0b
 platform 7: x00
 platform 8: xb0
 platform 9: xx0
 platform 10: x0b
 platform 11:x0x
 platform 12: 0b0
 platform 13: 0bb
 platform 14: 0bx
 platform 15: 0x0
 platform 16: 0xb
 platfrom 17: 0xx
 platform 18: 00b
 platform 19: 00x

Using this key I created 7 different "classes" that blocks fall into and which blocks cannot be adjacent to them.

 all clear: 
     contains: 1
     cannot adjacent: none
 left covered:
     contains: 2, 7
     cannot adjacent: 13, 14, 16, 17
 left & mid covered:
     contains: 3, 4, 8, 9
     cannot adjacent: 5, 6, 10, 11, 13, 14, 16, 17, 18, 19
 left & right covered:
     contains: 5, 6, 10, 11
     cannot adjacent: 3, 4, 8, 9, 12, 13, 14, 15, 16, 17
 middle covered:
     contains: 12, 15
     cannot adjacent: 5, 6, 10, 11
 middle & right covered: 
     contains: 13, 14, 16, 17
     cannot adjacent: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
 right covered:
     contains: 18, 19
     cannot adjacent: 3, 4, 8, 9

Using this information, I created 7 different GameObject[]'s that contain only blocks that are safe to the 7 areas. (i.e. in GameObject[] left_safe_walkways I have all platforms except for 13, 14, 16, and 17).


With all of this in mind, I created an IENumerator that currently spawns up to 100 platforms using a for loop. I have, since using code to limit the block spawns, have still been seeing impossible parts in the game. Is there anything I can do to fix it? Here's the code I am using currently: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class spawnwalkway : MonoBehaviour {
     public GameObject[] all_walkways;
     public GameObject[] left_safe_walkways;
     public GameObject[] left_mid_safe_walkways;
     public GameObject[] left_right_safe_walkways;
     public GameObject[] mid_safe_walkways;
     public GameObject[] mid_right_safe_walkways;
     public GameObject[] right_safe_walkways;
     public int walkway_number;
     public int next_walkway_number;
     int plat_num_random;
     // Use this for initialization
     void Start () {
 
         StartCoroutine(spawnBlock());
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     IEnumerator spawnBlock()
     {
         int walkway_number = Random.Range (0, 18);
         for (int z = 1; z < 100; z++)
         {
             if (walkway_number == 0) {
                 next_walkway_number = Random.Range (0, 18);
                 Debug.Log (all_walkways [next_walkway_number] + "block "+ z);
                 Instantiate (all_walkways [next_walkway_number], new Vector3 (-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
             } else
             if (walkway_number == 1 || walkway_number == 6) {
                 next_walkway_number = Random.Range (0, 14);
                 Debug.Log (left_safe_walkways [next_walkway_number] + "block "+ z);
                 Instantiate (left_safe_walkways [next_walkway_number], new Vector3 (-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
             } else
             if (walkway_number == 2 || walkway_number == 3 || walkway_number == 7 || walkway_number == 8) {
                 next_walkway_number = Random.Range (0, 8);
                 Debug.Log(left_mid_safe_walkways[next_walkway_number] + "block "+ z);
                 Instantiate (left_mid_safe_walkways [next_walkway_number], new Vector3 (-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
             } else
             if (walkway_number == 4 || walkway_number == 5 || walkway_number == 9 || walkway_number == 10) {
                 next_walkway_number = Random.Range (0, 8);
                 Debug.Log (left_right_safe_walkways [next_walkway_number] + "block "+ z);
                 Instantiate (left_right_safe_walkways [next_walkway_number], new Vector3 (-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
             } else
             if (walkway_number == 11 || walkway_number == 14) {
                 next_walkway_number = Random.Range (0, 14);
                 Debug.Log(mid_safe_walkways[next_walkway_number] + "block "+ z);
                 Instantiate (mid_safe_walkways [next_walkway_number], new Vector3 (-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
             } else
             if (walkway_number == 12 || walkway_number == 13 || walkway_number == 15 || walkway_number == 16) {
                 next_walkway_number = Random.Range (0, 8);
                 Debug.Log(mid_right_safe_walkways[next_walkway_number] + "block "+ z);
                 Instantiate (mid_right_safe_walkways [next_walkway_number], new Vector3 (-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
             } else
             if (walkway_number == 17 || walkway_number == 18) {
                 next_walkway_number = Random.Range (0, 14);
                 Debug.Log(right_safe_walkways[next_walkway_number] + "block "+ z);
                 Instantiate (right_safe_walkways [next_walkway_number], new Vector3 (-1.42637f, -0.4771186f, 2 * z + 8.574288f), Quaternion.identity);
             } 
 
             walkway_number = next_walkway_number;
             yield return new WaitForSeconds(0.5f);
         }
     }
 
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

102 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

Related Questions

Problems with spawning items that the players can pick up - Unet 0 Answers

I want to respawn the food everytime the snake collides with it 0 Answers

Instantiate over time? 1 Answer

How do I control where enemies spawn? 1 Answer

Problem with spawn object 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