Wrong position on spawn via script on every object (2.973684 instead of 3)
Hello,
im trying to build 2d game and on start there are spawning some squares via script. Somehow the width position on every square is incorrect. Instead of "-3" its "-3.026316" or "2.973684" and so on. The height is always the right round number.
Here is the code, please dont judge me, im a noob.
public struct Point
{
public float X { get; set; }
public float y { get; set; }
public Point(float x, float y)
{
this.X = x;
this.y = y;
}
}
public class TileScript : MonoBehaviour
{
// Points for grid position
public Point GridPosition { get; private set; }
public Vector2 WorldPosition
{
get
{
return new Vector2(transform.position.x + (GetComponent<SpriteRenderer>().bounds.size.x / 2), transform.position.y - (GetComponent<SpriteRenderer>().bounds.size.y / 2));
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void Setup(Point gridPos, Vector3 worldPos, Transform parent)
{
this.GridPosition = gridPos;
transform.position = worldPos;
transform.SetParent(parent);
LevelManager.Instance.Tiles.Add(gridPos, this);
}
private void OnMouseOver()
{
if (Input.GetMouseButtonDown(0))
{
PlaceTower();
}
}
private void PlaceTower()
{
Instantiate(GameManager.Instance.TowerPrefab, transform.position, Quaternion.identity);
}
}
public class LevelManager : Singleton<LevelManager>
{
//Prefab field
[SerializeField]
private GameObject [] tilePrefabs;
//Gives the start and end points a reference
private Point startSpawn, endSpawn, castleSpawn;
[SerializeField]
private GameObject startTilePrefab;
[SerializeField]
private GameObject endTilePrefab;
[SerializeField]
private Transform towerfield;
[SerializeField]
private GameObject castlePrefab;
[SerializeField]
private GameObject groundPrefab;
[SerializeField]
private GameObject canvas;
[SerializeField]
private GameObject towerPanelPrefab;
public Dictionary<Point, TileScript> Tiles { get; set; }
//Calculates the right tilesize for positioning
public float TileSize
{
get { return tilePrefabs[0].GetComponent<SpriteRenderer>().sprite.bounds.size.x; }
}
// Start is called before the first frame update
void Start()
{
CreateLevel();
}
// Update is called once per frame
void Update()
{
}
//Creates the towerfield
private void CreateLevel()
{
Tiles = new Dictionary<Point, TileScript>();
// Prefab order
string[] mapData = ReadTowerfield();
//nosense? maybe later
int mapX = mapData[0].ToCharArray().Length;
int mapY = mapData.Length;
// Variable to find startpoint on grid
Vector3 worldStart = Camera.main.ScreenToWorldPoint(new Vector3 (0, 0));
for (int y = 0; y < 17; y++) // y positions
{
char[] newTiles = mapData[y].ToCharArray();
for (int x = 0; x < 7; x++) // x positions
{
PlaceTile(newTiles[x].ToString(), x, y, worldStart);
}
}
// Instanciate the start and end points
spawnPoints();
spawnCastle();
spawnGround();
}
// Tile method
private void PlaceTile(string tileType, int x, int y, Vector3 worldStart)
{
// String to Int
int tileIndex = int.Parse(tileType);
//Creates a new tile and link it to the prefab
TileScript newTile = Instantiate(tilePrefabs[tileIndex]).GetComponent<TileScript>();
//Gives a tile a x&y position
newTile.Setup(new Point(x, y), new Vector3(worldStart.x + 0.5f + (TileSize * x), worldStart.y + (TileSize * y), 0),towerfield);
}
// Is Reading the text file for easy mapdesign
private string[] ReadTowerfield()
{
// Loads the file
TextAsset data = Resources.Load("Towerfield") as TextAsset;
// Is changing the text to a single line
string tmpData = data.text.Replace(Environment.NewLine, string.Empty);
// Is needed for easy visual mapdesign in text file
return tmpData.Split('-');
}
//Creates the start and end point
private void spawnPoints()
{
startSpawn = new Point(3, 1);
Instantiate(startTilePrefab, Tiles[startSpawn].transform.position,Quaternion.identity);
endSpawn = new Point(3, 16);
Instantiate(endTilePrefab, Tiles[endSpawn].transform.position, Quaternion.identity);
}
private void spawnCastle()
{
castleSpawn = new Point(3, 14);
Instantiate(castlePrefab, Tiles[castleSpawn].transform.position, Quaternion.identity);
}
private void spawnGround()
{
castleSpawn = new Point(0, 0);
Instantiate(groundPrefab, Tiles[castleSpawn].transform.position, Quaternion.identity);
}
}
Comment