- Home /
How can i instantiate gameobjects to be position on the left edge side of the terrain, from left to right ?
For example i manually added 4 cubes on the left side of the terrain. Cube(1) is on the most left side on the edge. Then Cube(2)....(3)...(4) This is how i want to instantiate the gameobjects in the script. Each time i click on a key and it's instantiating a new gameobject i want the first gameobject to be on the left top corner of the terrain and the next the gameobject near the first one on it's right and so on...When the last gameobject is near or on the right edge jump like to a new line and instantiate the next gameobjects again from left to right.
You can cube(4) then for example i will add more cubes and more until it will get to the right edge of the terrain then i want it automatic when i will instantiate new gameobjects they will start from the left side again under the existing gameobjects like in a new line.
This is a screenshot example showing what i mean new line. Cube(21) is marked wiith black circle and Cube(22) start a new line from the left side.
This is the script i have attached to a empty gameobject:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
public class ClickOn : MonoBehaviour
{
public GameObject[] prefabs;
public int gap = 10;
private GameObject go;
private GameObject tests;
void Start()
{
prefabs = Resources.LoadAll("Prefabs", typeof(GameObject)).Cast<GameObject>().ToArray();
foreach (GameObject prefab in prefabs)
{
if (prefab.name == "My Prefabs")
{
go = prefab;
}
}
tests = GameObject.Find("Testing");
}
void Update()
{
if (Input.anyKey)
{
foreach (Transform child in go.transform)
{
if (Input.inputString == child.name)
{
Transform clone = Instantiate(child, new Vector3(gap += 50, 0, 0), Quaternion.identity);
clone.parent = tests.transform;
}
}
}
}
}
The problem is now when i click on any key and it's instantiating new gameobject it's starting from the right side of the terrain from position 0,0,0
But i want to find/calculate the left top corner position of the terrain according to the terrain size width height and start instantiating from the left side.
Your answer
Follow this Question
Related Questions
How can i prevent from mouse to collide with thirdpersoncontroller ? 0 Answers
How can i change the walls height and keep the size for example 100x100 but height 0.1 ? 1 Answer
How can i using a break point if a gameobject have a collider after added to it ? 1 Answer
How can i get all childs from List ? 3 Answers
How can i set the same script on two GameObjects so the script will work on both objects same time ? 1 Answer