- Home /
How to spawn objects within a planes dimensions
I posted an example of what im trying to accomplish. Any help is greatly appreciated!
Answer by Cornelis-de-Jager · Jan 14, 2019 at 09:30 PM
The trick here is to not spawn it in within the dimensions initially. But rather manipulate it once you have spawned it. That way you don't have to worry about rotational offset.
public Transform plane;
public GameObject spawnablePrefab;
// Plane Properties
float x_dim;
float z_dim;
void Start () {
// Get the length and width of the plane
x_dim = plane.size.x;
z_dim = plane.size.z;
}
void Spawn () {
// Spawn the object as a child of the plane. This will solve any rotation issues
GameObject obj = Instantiate (spawnablePrefab, Vector3.zero, Qauternion.Identity, plane) as GameObject;
/* Move the object to where you want withing in the dimensions of the plane */
// random the x and z position between bounds
var x_rand = Random.Range(-x_dim, x_dim);
var z_rand = Random.Range(-z_dim, z_dim);
// Random the y position from the smallest bewteen x and z
var z_rand = x_rand > z_rand ? Random.Range(0, z_rand) : Random.Range(0, x_rand);
// Now move the object
// Since the object is a child of the plane it will automatically handle rotational offset
obj.transform.position = new Vector3 (x_rand, y_rand, z_rand);
// Now unassign the parent
obj.parent = null;
}
hahaha, u post a solution where I was testing a answer script whatever it is, uses size of the meshes... almost the same
The most easy approach is, I believe, measure manually the "square" and put this on a script in the plane object, u can set the variables on inspector, or measure a mesh plane attached to gameobject,
the following script measure it on start, delete start function to set custom plane measures by inspector, and the update function is to test...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SquareScript : $$anonymous$$onoBehaviour {
public float dimX;
public float dimY;
public GameObject spawntest;
int itest = 0;
void Start () {
$$anonymous$$esh _mesh = transform.GetComponent<$$anonymous$$eshFilter> ().mesh;
dimX = _mesh.bounds.size.x;
dimY = _mesh.bounds.size.z;//I assume here that the square is on x and z, otherwise replace for the correct plane axis
}
//to test purpose, attach to default plane and set a gameobject on spawntest variable
void Update(){
if (itest < 5){
SpawnInside (spawntest);
itest++;
}
}
public void SpawnInside(GameObject spawnObject){
Vector3 randpos = Vector3.zero;
randpos.x = Random.Range(-dimX/2f, dimX/2f);//assume mesh of the plane is centered, view mesh.bounds.$$anonymous$$.x and mesh.bounds.max.x if not centered
randpos.y = 0f;//"level" hoy much up to the plane spawn the objects
randpos.z = Random.Range(-dimY/2f, dimY/2f);
Transform instance = Instantiate (spawnObject, this.transform).transform;
instance.localPosition = randpos;
}
}
Your answer
Follow this Question
Related Questions
Position in Vector (C#) 1 Answer
Cannot implicitly convert type 'int' to 'UnityEngine.Vector3' 3 Answers
split vector3 coordinates in to x,y,z 1 Answer
collider triggers transform position 1 Answer
Vector3.Lerp works outside of Update() 3 Answers