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 /
  • Help Room /
This question was closed Jan 19, 2017 at 01:17 AM by Kernivorous for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Kernivorous · Jan 18, 2017 at 10:24 PM · 2d gameraycasthit2drandom gen

Why does this code run infinitely?

From what I can see, this code should only run 20 time but it keeps generating rooms forever.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class RoomGeneration : MonoBehaviour {

 public int roomsGenerated; // How many rooms are generated

 public GameObject[] rooms;
 public GameObject doorTop;
 public GameObject doorBottom;
 public GameObject doorLeft;
 public GameObject doorRight;
 public GameObject rayUp;
 public GameObject rayDown;
 public GameObject rayLeft;
 public GameObject rayRight;

 int genX; // Room X coord
 int genY; // Room Y coord

 // Use this for initialization
 void Start()
 {

     genX = 0;
     genY = 0;

     Instantiate(rooms[0], new Vector2(genX, genY), Quaternion.identity);
 }

 // Update is called once per frame
 void Update()
 {
    for(int i = 0; i <= 20; i++)
     {
         CheckAndBuild();
     }
 }

 void CheckAndBuild()
 {
     int randomRoomDir = Random.Range(0, 4);
     int randomRoom = Random.Range(1, 6);

     RaycastHit2D hitUp = Physics2D.Raycast(rayUp.transform.position, transform.up, 3f);
     RaycastHit2D hitDown = Physics2D.Raycast(rayDown.transform.position, transform.up * -1, 3f);
     RaycastHit2D hitLeft = Physics2D.Raycast(rayLeft.transform.position, transform.right * -1, 5f);
     RaycastHit2D hitRight = Physics2D.Raycast(rayRight.transform.position, transform.right, 5f);
     Debug.DrawRay(rayUp.transform.position, transform.up * 3f, Color.red, 1000f);
     Debug.DrawRay(rayDown.transform.position, transform.up * -1 * 3f, Color.red, 1000f);
     Debug.DrawRay(rayLeft.transform.position, transform.right * -1 * 5f, Color.red, 1000f);
     Debug.DrawRay(rayRight.transform.position, transform.right * 5f, Color.red, 1000f);


     if (randomRoomDir == 0 && hitUp.collider == null) // Generate room above
     {
         Instantiate(doorTop, new Vector2(genX, genY + 3.55f), Quaternion.identity);
         genY += 10;
         Vector2 roomGenPos = new Vector2(genX, genY);
         transform.position = roomGenPos;
         Instantiate(rooms[randomRoom], roomGenPos, Quaternion.identity);
         Instantiate(doorBottom, new Vector2(genX, genY - 3.55f), Quaternion.identity);

         print(hitUp.collider);
     }

     if (randomRoomDir == 1 && hitDown.collider == null) // Generate room below
     {
         Instantiate(doorBottom, new Vector2(genX, genY - 3.55f), Quaternion.identity);
         genY -= 10;
         Vector2 roomGenPos = new Vector2(genX, genY);
         transform.position = roomGenPos;
         Instantiate(rooms[randomRoom], roomGenPos, Quaternion.identity);
         Instantiate(doorTop, new Vector2(genX, genY + 3.55f), Quaternion.identity);
         print(hitUp.collider);
     }

     if (randomRoomDir == 2 && hitLeft.collider == null) // Generate room to the left
     {
         Instantiate(doorLeft, new Vector2(genX - 7.4f, genY), Quaternion.identity);
         genX -= 20;
         Vector2 roomGenPos = new Vector2(genX, genY);
         transform.position = roomGenPos;
         Instantiate(rooms[randomRoom], roomGenPos, Quaternion.identity);
         Instantiate(doorRight, new Vector2(genX + 7.4f, genY), Quaternion.identity);
         print(hitUp.collider);
     }

     if (randomRoomDir == 3 && hitRight.collider == null) // Generate room to the right
     {
         Instantiate(doorRight, new Vector2(genX + 7.4f, genY), Quaternion.identity);
         genX += 20;
         Instantiate(rooms[randomRoom], new Vector2(genX, genY), Quaternion.identity);
         Instantiate(doorLeft, new Vector2(genX - 7.4f, genY), Quaternion.identity);
         print(hitUp.collider);
     }
 }

}

Comment
Add comment · Show 5
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 Kernivorous · Jan 18, 2017 at 10:28 PM 0
Share

Also, my raycasts aren't hitting any colliders. There are four raycasts (top, left, right and bottom) that are meant to check if there is a room there and if there isn't, it can generate a room there. It doesn't seem to detect any rooms though so rooms are spawned on top of other rooms sometimes.

avatar image doublemax · Jan 19, 2017 at 12:20 AM 0
Share

Update() is called every frame. You probably want to move the initialization code to Start():

avatar image Kernivorous doublemax · Jan 19, 2017 at 12:53 AM 0
Share

Yeah but shouldn't CheckAndBuild() stop running once i = 20?

avatar image Kernivorous Kernivorous · Jan 19, 2017 at 12:54 AM 0
Share

Or is i being created and set to 0 every frame?

Show more comments

1 Reply

  • Sort: 
avatar image
0

Answer by Kernivorous · Jan 19, 2017 at 01:17 AM

Ok this now solved. Thanks doublemax :D

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

Follow this Question

Answers Answers and Comments

93 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

Related Questions

Why aren't raycasts hitting any colliders? 0 Answers

Problem with random room gen using raycasts! 0 Answers

RayCastHit2D fails to hit script generated grid 1 Answer

Stucking with RTS Building detection using 2D Raycasts and 2DBoxColliders 0 Answers

Vertical Rays rapidly changing between "detecting ground" to "not detecting ground" 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