Why when spawn cubes inside a specific area at random positions they are not inside the area ?
The whole position of the cubes area is not inside the walls.
This is the script that create the walls:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallsTest : MonoBehaviour
{
public GameObject prefab;
public Vector3 wallsStartPosition;
public float width = 0;
public float height = 1;
public float lengthX = 2;
public float lengthZ = 2;
public float time = 1;
public Camera wallsCamera;
private List<GameObject> walls = new List<GameObject>();
private IEnumerator coroutine;
void Start()
{
wallsCamera.transform.position = new Vector3(wallsStartPosition.x, wallsStartPosition.y + 100, wallsStartPosition.z - 250);
for (int i = -2; i < 2; i++)
{
GameObject go = Instantiate(prefab);
go.transform.parent = transform;
Vector3 scale = Vector3.one;
Vector3 adjustedPosition = wallsStartPosition;
float sign = Mathf.Sign(i);
if ((i * sign) % 2 == 0)
{
adjustedPosition.x += (lengthX * sign) / 2;
scale.x = width;
scale.y = height;
scale.z *= lengthZ + width;
}
else
{
adjustedPosition.z += (lengthZ * sign) / 2;
scale.x *= lengthX + width;
scale.y = height;
scale.z = width;
}
adjustedPosition.y -= height / 2;
go.transform.localScale = scale;
go.transform.localPosition = adjustedPosition;
walls.Add(go);
}
coroutine = raiseWall(walls[0], height, time, 0, 100);
StartCoroutine(coroutine);
coroutine = raiseWall(walls[2], height, time, 0, 100);
StartCoroutine(coroutine);
coroutine = raiseWall(walls[1], height, time, 0, 100);
StartCoroutine(coroutine);
coroutine = raiseWall(walls[3], height, time, 0, 100);
StartCoroutine(coroutine);
}
IEnumerator raiseWall(GameObject wall, float amount, float time, float delay, float steps)
{
Vector3 position = wall.transform.localPosition;
float finalHeight = position.y + amount;
yield return new WaitForSeconds(delay);
while (position.y < finalHeight)
{
float remainingHeight = finalHeight - position.y;
position.y += Mathf.Min(remainingHeight, amount / steps);
wall.transform.localPosition = position;
yield return new WaitForSeconds(time / steps);
}
yield break;
}
}
And this is the script that spawn the cubes at random positions but i want them to be inside the walls area:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnObjects : MonoBehaviour
{
public int numberOfObjects;
private int currentObjects;
public GameObject objectToPlace;
private int wallsLengthX;
private int wallsLengthZ;
private int wallsPosX;
private int wallsPosZ;
void Start()
{
var wi = GetComponent<WallsTest>();
wallsLengthX = (int)wi.lengthX;
wallsLengthZ = (int)wi.lengthZ;
wallsPosX = (int)wi.wallsStartPosition.x;
wallsPosZ = (int)wi.wallsStartPosition.z;
}
// Update is called once per frame
void Update()
{
if (currentObjects <= numberOfObjects)
{
int posx = Random.Range(wallsPosX, wallsPosX + wallsLengthX);
int posz = Random.Range(wallsPosZ, wallsPosZ + wallsLengthZ);
float posy = Terrain.activeTerrain.SampleHeight(new Vector3(posx, 0, posz));
GameObject newObject = (GameObject)Instantiate(objectToPlace, new Vector3(posx, posy, posz), Quaternion.identity);
newObject.transform.localScale = new Vector3(5, 5, 5);
currentObjects += 1;
}
if (currentObjects == numberOfObjects)
{
Debug.Log("Generate objects complete!");
}
}
}
try adding this before your spawn:
float posx = Random.Range(wallsPosX, wallsPosX + wallsLengthX);
float posz = Random.Range(wallsPosZ, wallsPosZ + wallsLengthZ);
posx+=wallsLenghtX*-0.5f;
posy+=wallsLengthZ*-0.5f;
//now spawn here
Worked fine on the first time on the second time i got this: One of the cubes spawn on the edge of one of the walls so half of the cube is inside but the other half of the cube is outside.
How can i fix this kind of problem ? I had it before, but not sure how to fix it.
you would need to add half the cube's width to the random range's start and subtract half the cube's width from the random range's limit.
the cubes position is actual where the middle of the cube sits. So the script as you have it, is not allowing the CENTER of the cube out of bounds.
Your answer
Follow this Question
Related Questions
I create material with script but it does not render right 0 Answers
Creating Splines from empties in script 0 Answers
How can i rotate all the child objects together at the same time ? 1 Answer
How can i give another name/number to the created Plane object name ? 0 Answers
How can i create List of maps from each Map class ? 0 Answers