2
\$\begingroup\$

I've recently been learning C# and I wanted to take one of my 2D tutorials to Unity.

I reached a step where I need to spawn my new sprite into a collision free location. I've followed direction - generated random location, calculating Vector2s using the location and collider, and then used a while loop to check Physics2D.OverlapArea using my 2 vectors.

But, because I'm still only just learning C#, I'm finding it difficult to know if I have actually written my code correctly. Is there any simple way of being able to figure this out? Using Debug or something?

Vector3 spawnLocation = new Vector3 (Random.Range (minSpawnX, maxSpawnX),
                                     Random.Range (minSpawnY, maxSpawnY),
                                     -Camera.main.transform.position.z);
Vector3 spawnPoint = Camera.main.ScreenToWorldPoint (spawnLocation);
Vector2 lowerLeftCorner = new Vector2 (spawnPoint.x - teddyBearColliderHalfWidth,
                                       spawnPoint.y - teddyBearColliderHalfHeight);
Vector2 upperRightCorner = new Vector2 (spawnPoint.x + teddyBearColliderHalfWidth,
                                        spawnPoint.y + teddyBearColliderHalfHeight);
while (Physics2D.OverlapArea (lowerLeftCorner, upperRightCorner) != null) 
{
    spawnPoint.x = Random.Range(minSpawnX, maxSpawnX);
    spawnPoint.y = Random.Range(minSpawnY, maxSpawnY);

    lowerLeftCorner = new Vector2 (spawnPoint.x - teddyBearColliderHalfWidth,
                                   spawnPoint.y - teddyBearColliderHalfHeight);
    upperRightCorner = new Vector2 (spawnPoint.x + teddyBearColliderHalfWidth,
                                    spawnPoint.y + teddyBearColliderHalfHeight);
}
Debug.Log (Physics2D.OverlapArea(lowerLeftCorner, upperRightCorner));

I was trying to use a debug at the end there, but it doesn't seem to continue running every single time that there's a spawn, so I am still unsure if it is working. I'm terribly sorry for my nooby code. Feel free to direct to me some place else if you feel I need to do so.

\$\endgroup\$
1
\$\begingroup\$

Of course you can check the syntax automatically using an IDE like Visual Studio. However, without some more work the only measure of correctness is "does it do what you expect?" If it doesn't, then you would debug to find out why.

Of course, you can take it a step further and apply Unit Testing. This is where you write code that sets up scenarios where you know there will be an expected result. For example, if you had a method like

int Add(int left, int right) { return left + right; }

you could write unit tests like

bool TestAdd() { return Add(2, 2) == 4; }

You then run all these tests in a separate mode and take note of which ones fail, or you can use an external project that links in your code to run all the methods. Most Unit testing APIs like NUnit provide decorations so their test engines can automatically extract, run, and gather results from your tests.

Check out "Unit Tests By The Book" part 1 and part 2 on the Unity Blog.

\$\endgroup\$
1
\$\begingroup\$

Work Smarter not harder! For this you can do a simple trick. Drop empty GameObjects in the environment where you want to spawn your sprite. Take them in a public List or Array and Spawn at the random GameObject's Transform of List or Array. For visibility drop cubes instead of empty GameObjects and after setting them disable their mesh and colliders.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .