How to instantiate objects to mesh while restricting the spawn area on a single axis?
Currently i have it setup so i'm able to instantiate my object onto the mesh by using a raycast to cast down to the mesh's surface. The problem with this is that my instantiated objects can spawn at undesired locations, like the side of mountains or under the water. How can i go about restricting the spawn on a single axis (in this instance i need the .y of the instantiated object to be clamped based upon certain values)
Issue
Thanks in advance.
public class TreeGenerator : MonoBehaviour {
public int numberOfObjects;
public int currentObjects;
public GameObject treeToPlace;
private float randomX;
private float randomZ;
private Renderer r;
void Start() {
r = GetComponent<Renderer>();
}
void Update() {
RaycastHit hit;
if(currentObjects <= numberOfObjects) {
randomX = Random.Range(r.bounds.min.x, r.bounds.max.x);
randomZ = Random.Range(r.bounds.min.z, r.bounds.max.z);
if (Physics.Raycast(new Vector3(randomX, r.bounds.max.y - 5f, randomZ), -Vector3.up, out hit)) {
Instantiate(treeToPlace, hit.point, Quaternion.Euler(0, Random.Range(0, 360), 0));
currentObjects += 1;
}
}
}
}
Answer by tormentoarmagedoom · May 10, 2018 at 09:35 AM
Good day.
I recoomend you to store the new instantiated object when instantiating. So you can modyfy all you want just at thse same frame is instantiated.
Replace your instantiate code line for this:
GameObject NewTree = Instantiate(treeToPlace, hit.point, Quaternion.Euler(0, Random.Range(0, 360), 0)) as GameObject;
So now, you have your new tree gameobject in the variable "NewTree", and you can change anything as any other gameobject for example:
NewTree.transform.position = new Vector3(NewTree.transform.position.x, 15, NewTree.transform.position.z);
This also respond how to change the variable Y from the position vector. (Create a new vector, assign the variables from the transfomr you need and the number of the variable you want to change)
Bye!
:D
If helped, accept the answer
Thanks for the speedy response. This seems like a more sensible solution than the one i came up with, thanks for your help! :)
Answer by Jezzywezzy101 · May 10, 2018 at 09:37 AM
Found a solution for anyone that may need this in the future.
I created a mesh collider on my water plane and add a new tag to it called 'Water'. I then did the same thing for my terrain (mesh) called the tag 'Ground'
from there i simply added && hit.transform.tag == "Ground" to my raycast IF Statement.
void Update()
{
RaycastHit hit;
if (currentObjects <= numberOfObjects)
{
randomX = Random.Range(r.bounds.min.x, r.bounds.max.x);
randomZ = Random.Range(r.bounds.min.z, r.bounds.max.z);
if (Physics.Raycast(new Vector3(randomX, r.bounds.max.y, randomZ), -Vector3.up, out hit) && hit.transform.tag == "Ground")
{
Instantiate(treeToPlace, hit.point, Quaternion.Euler(0, Random.Range(0, 360), 0));
currentObjects += 1;
}
//Instantiate(treeToPlace, new Vector3(randomX, Random.Range(minY, maxY), randomZ), transform.rotation);
//currentObjects += 1;
}
}
}
Your answer
Follow this Question
Related Questions
Animated rigged blender model generating colliders problem 1 Answer
How can I recalculate mesh collider to fit mesh? 0 Answers
How do I make it so the rigibody on the parent uses the mesh collider on the children ? 0 Answers
Prefabs aren't instantiating where they should be 0 Answers
How to implement dart game? 0 Answers