- Home /
Instantiate an object next to an object
Hello programmers,
I would like to implement that when I have placed an object let's say on a grid and if I click on the next tile they are coming inside each other. So what I think that should work is to check all the sides of the object and if it has collision, then place it next to the object automatically.
Here is a snippet of my code:
if (EditorManager.instance.value == EditorManager.Mode.UIMode) {
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
{
if (Input.GetMouseButton(0))
{
try
{
int x = hit.collider.GetComponent<Tile>().getX;
int y = hit.collider.GetComponent<Tile>().getY;
mapxml[x, y].SetTileType(1);
wallObject = MonoBehaviour.Instantiate(wall, new Vector3(hit.transform.position.x,
hit.transform.position.y + 0.5f, hit.transform.position.z), Quaternion.identity) as GameObject;
bool valid = true;
foreach (GameObject b in walls)
{
if (wallObject.transform.collider.bounds.Intersects(b.collider.bounds))
{
GameObject.DestroyImmediate(wallObject);
valid = false;
break;
}
}
if (valid && wallObject != null)
{
walls.Add(wallObject);
}
SetMap();
}
Answer by frogsbo · Jan 16, 2014 at 01:05 PM
if you have less than 10 000 objects to place on grid, put them in an array, and after racast has position, compare any new tiles to positions of tiles already in array, and if same, place it on x+1 and check also if there is one tile there. use tags for example for tiles, find object with tag tile, for each tile in tiles... if pos = pos tile... etc
other option is sphere cast. cast a sphere physics around the position and check all tiles a spherical radius of for example 2.
I have them currently in a list where I add them and when they have intersection I simply remove it but what's the next step?
I have updated my script so take a look and tell me what is next
Great work! i would check if the clicked position already has a tile before making one there and then destroying it if you have to. it saves a step
I would do this:
get hitpoint.position
var alreadythere :int = 0;
for wall in walls{
if(wall.position == hit point){ alreadythere =1}
}
if alreadythere = 0, instantiate new tile on hitpoint.
if alreadythere <=1, instantiate new tile on (hitpoint)+Vector3(1,0,0)//simple way to do it
______________________________________________________________
complex way to do it...
if alreadythere <= 1, make 4 loops to check the neighbour spaces if there is a tile there. if one of them is alreadythere=0, instantiate tile on first free space
out of 4 neigbours, even 8 if you want i already did alot of this with this one:" http://www.youtube.com/watch?v=$$anonymous$$HpIb6iDv$$anonymous$$Q
The video is indeed a great example what I almost achieved but I made it more complexer then you would think, anyway I almost fixed it by myself so thanks for commenting and trying to point me in the right direction
Your answer
Follow this Question
Related Questions
How to apply more than one trigger on a single gamebject 1 Answer
Grid Movement VS Free Movement 1 Answer
2D Tile Map Question 0 Answers
Generating and altering a mesh at runtime as a grid based terrain 3 Answers
Clicking on objects 2 Answers