Spawning ontop of each other

Hello Everyone,

So far so good ! Except that I can spawn on-top of my defenders. Not sure how to fix this. Been tinkering around with no luck.

My canvas has a different Z axis , my defenders have the same z axis and layer (except for star on the trophy- different layer) , they all have box colliders with triggers.

When I go in my Defender script - they don’t detect collisions with one another (when spawned ontop) - was going to destroy the object if spawned ontop of each other but that wont work.

Any suggestions?

Thanks.

@Joao_Dalvi help appreciated! lol

2 Likes

Summoning…
20%
40%
60%
80%
100%
Johnny Spawned…

(sorry, always wanted to do that :joy::joy::joy::joy: )

Hello Chris, how are you?

Would you mind pasting the script responsible for spawning the defenders?
I don’t remember the structure.

3 Likes

Ohh I got it…

I re-watched the video from part 18:18 and it made more sense 8th time around(haha)

the box collides are blocking the screenToWorldPort from creating something on the game space.

This issue with this is, my box colliders are the size of my instantiated sprites. I would have to make it so it takes up the whole square play space in order to prevent clicking at the edges of the blocks to create extra clones.

Is this the best way for now?

Thanks…

2 Likes

HAHAHA! Awesome.

Sure- this script should help refresh memory. It is the one that instantiates the defenders to the scene.

public class DefenderSpawner : MonoBehaviour
{
public Camera myCamera;
GameObject parent;

void Start ()
{
	parent = GameObject.Find ("DefenderParent");
	if (parent == null) 
	{
		parent = new GameObject ("DefenderParent"); 
	}
	
}
void OnMouseDown()
{
	Vector2 pos = SnapToGrid (worldPosRaw ()); 
	Instantiate (Buttons.selectedDefender, pos, Quaternion.identity);
}

public Vector2 worldPosRaw ()
{
	float rawX = Input.mousePosition.x;
	float rawY = Input.mousePosition.y;
	Vector3 rawPositions = new Vector3 (rawX, rawY,10f);
	Vector2 snapMe = myCamera.ScreenToWorldPoint (rawPositions);
	return snapMe; 
}
public Vector2 SnapToGrid(Vector2 oldPos)
{
	float newX = Mathf.RoundToInt (oldPos.x);
	float newY = Mathf.RoundToInt (oldPos.y); 
	Vector3 snappedPos = new Vector3 (newX, newY, 10f); 
	return snappedPos;
}

}

2 Likes

Oh, glad that you found the problem :slight_smile:

This is the structure that Ben made for this section (and fits pretty well to the learning curve), so the solution pointed in the class does fit the overall game structure.
There are better options IMO, but would require more advanced tile based structures to work.

2 Likes

I’ll just go with this for now then : ) . Thanks for looking into it.

Happy Thursday!

2 Likes

No problem at all,

Just let me know if you need something else, Happy Thursday over there too!

1 Like

ROTFL! :smiley:

1 Like

Hi Everyone,

Same issue i have got! i coundn’t solve it, no matter what i’ve done.

My Canvas at position z=1
Defenders at position z=0

DefenderSpawner script is exactly same, Box colliders are exact square.
But still i can spawn my defenders on top of each

Do you have any additional idea ?

Thanks

I’m encountering the same problem. The defenders boxes are squared out, but still I have a defender now and then who can be placed on another. I’m using Ben’s defspawn script. (Im bookmarking this thread for later resolve)

I think I’ve got my double spawn issue solved (tnx to an older thread).
I’ve ran a few tests, but so far no more issues.

Camera at -10 z, Level canvas at +5 Z, de rest at 0.
And the defenders boxes squared.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.