Hi
I’m trying to build a script that generates hexagons with the click of a button. But whenever they spawn all of them are in the wrong x position because a float isn’t being added in my script. I included the relevant part of my script.
The problem is with the lines that say “xPosition = xBase * 1732051 / 1000000 + 1732051 / 1000000 / 2;”
For some reason it will multiply 1732051 / 1000000 to the x base but not add the second part. I tried making a public variable/float that was equal to that number and adding that and I also tested multiplying it but for some reason unity registers it as a zero.
I really don’t know what to do here none of the other answers really covered this particular issue. Please help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawn : MonoBehaviour
{
//Hexagons: water, dirt, rock
public GameObject Water;
public GameObject Dirt;
public GameObject Rock;
public GameObject spawnButton;
public float xPosition;
public float yPosition;
public float zPosition;
public float multNumber = 1732051 / 1000000 / 2;
public float position;
public float positionNumber;
public float xBase;
public float xBase2;
public float zBase;
public float sphereRadius;
public float maxDistance;
public LayerMask layerMask;
private Vector3 objectPosition;
private Vector3 direction;
void Start()
{
}
void Update()
{
}
public void startSpawn()
{
//functions the button calls on click
StartCoroutine(SpawnObjects());
StopCoroutine(SpawnObjects());
Destroy(spawnButton);
}
Vector3 pickPosition()
{
positionNumber = Mathf.Round(Random.Range(0, 7));
//each of these (0-6) generates in a different z range bc the hexagons have to be placed at different x values to fit together
if (positionNumber == 0)
{
xBase = Mathf.Round(Random.Range(-5, 5));
yPosition = Mathf.Round(Random.Range(50, 50));
zPosition = Mathf.Round(Random.Range(0, 0));
xPosition = xBase * 1732051 / 1000000;
}
if (positionNumber == 1)
{
xBase = Mathf.Round(Random.Range(-5, 5));
Debug.Log(xBase);
yPosition = Mathf.Round(Random.Range(50, 50));
zBase = Mathf.Round(Random.Range(1, 1));
zPosition = zBase * 3 / 2;
xPosition = xBase * 1732051 / 1000000 + 1732051 / 1000000 / 2;
Debug.Log(xPosition);
}
if (positionNumber == 2)
{
xBase = Mathf.Round(Random.Range(-5, 5));
yPosition = Mathf.Round(Random.Range(50, 50));
zBase = Mathf.Round(Random.Range(0, 3));
zPosition = zBase * 3;
xPosition = xBase * 1732051 / 1000000;
}
if (positionNumber == 3)
{
xBase = Mathf.Round(Random.Range(-5, 5));
Debug.Log(xBase);
yPosition = Mathf.Round(Random.Range(50, 50));
zBase = Mathf.Round(Random.Range(0, 3));
zPosition = (zBase * 2 - 1) * 3 / 2;
xPosition = xBase * 1732051 / 1000000 + 1732051 / 1000000 / 2;
Debug.Log(xPosition);
}
//negatives
if (positionNumber == 4)
{
xBase = Mathf.Round(Random.Range(-5, 5));
Debug.Log(xBase);
yPosition = Mathf.Round(Random.Range(50, 50));
zBase = Mathf.Round(Random.Range(-1, -1));
zPosition = zBase * 3 / 2;
xPosition = xBase * 1732051 / 1000000 + 1732051 / 1000000 / 2;
Debug.Log(xPosition);
}
if (positionNumber == 5)
{
xBase = Mathf.Round(Random.Range(-5, 5));
yPosition = Mathf.Round(Random.Range(50, 50));
zBase = Mathf.Round(Random.Range(-3, 0));
zPosition = zBase * 3;
xPosition = xBase * 1732051 / 1000000;
}
if (positionNumber == 6)
{
xBase = Mathf.Round(Random.Range(-5, 5));
Debug.Log(xBase);
yPosition = Mathf.Round(Random.Range(50, 50));
zBase = Mathf.Round(Random.Range(-3, 0));
zPosition = (zBase * 2 + 1) * 3 / 2;
xPosition = xBase * 1732051 / 1000000 + 1732051 / 1000000 / 2;
Debug.Log(xPosition);
}
Vector3 objectPosition = new Vector3(xPosition, yPosition, zPosition);
RaycastHit hit;
direction = transform.forward;
sphereRadius = 1/8;
maxDistance = 0;
//this below is what is supposed to stop hexagons from spawning same place
if (Physics.SphereCast(objectPosition, sphereRadius, direction, out hit, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
{
pickPosition();
}
return objectPosition;
}
}
}