Quantcast
Channel: Questions in topic: "loops"
Viewing all articles
Browse latest Browse all 184

Infinite loop being caused by instantiating through an if statement

$
0
0
This is a hard one and I am having a tough time just formulating the question. Sorry in advance for the bad looking code and for possible noob mistakes, I am mostly self-taught. I'm building a terrain generator based on 2D blocks. The expected behaviour is that I'm placing a column of blocks and then each one of the blocks has a script based on if statements to automate the creation of a hill like structure. So each one of the blocks checks if it has an upper block, then a right and a left block. If an upper block exists and no right or no left block exist then a right or a left block is created. I am doing this using raycasthits, if the raycast hits a collider then it knows there is a block. What is hapenning right now is that I cannot for the life of me get rid of a loop that is creating left and right blocks infinitely despite the fact that there already is a left or a right block. It should test the neighboorhood and check that a block already exists and don't do anything, but instead it is just creating blocks with no respect whatsoever to the if statements Here is the script code using UnityEngine; using System.Collections; public class CornerBlockInstance : MonoBehaviour { public BoxCollider2D block; RaycastHit2D uhit; RaycastHit2D lhit; RaycastHit2D rhit; void Start () { TerrainGeneration (); } void TerrainGeneration() { uhit = Physics2D.Raycast (new Vector2 (transform.position.x + 0.5f, transform.position.y + 1.1f), new Vector2 (0, 0.5f), 0.5f); lhit = Physics2D.Raycast (new Vector2 (transform.position.x - 0.1f, transform.position.y + 0.5f), new Vector2 (-0.5f, 0), 0.5f); rhit = Physics2D.Raycast (new Vector2 (transform.position.x + 1.1f, transform.position.y + 0.5f), new Vector2 (0.5f, 0), 0.5f); Debug.DrawRay (new Vector2 (transform.position.x + 0.5f, transform.position.y + 1.1f), new Vector2 (0, 0.5f), Color.blue, 200); Debug.DrawRay (new Vector2 (transform.position.x - 0.1f, transform.position.y + 0.5f), new Vector2 (-0.5f, 0), Color.blue, 200); Debug.DrawRay (new Vector2 (transform.position.x + 1.1f, transform.position.y + 0.5f), new Vector2 (0.5f, 0), Color.blue, 200); if (uhit) { if (lhit != null) { BoxCollider2D heightBlock; heightBlock = Instantiate (block, new Vector2 (transform.position.x - 1f, transform.position.y), transform.rotation) as BoxCollider2D; Debug.Log (lhit.transform.position); } if (rhit == null) { BoxCollider2D heightBlock1; //instancia um bloco heightBlock1 = Instantiate (block, new Vector2 (transform.position.x + 1f, transform.position.y), transform.rotation) as BoxCollider2D; Debug.Log (rhit.transform.position); } } } I am not sure where the problem resides in my code but I think it might have something to do with the if statements and the fact that I am checking for a null raycast hit. Thanks in advance.

Viewing all articles
Browse latest Browse all 184

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>