Place tree on terrain. RayCast find hit.point.y only works once.
I am having some trouble with this script. All of the trees are added to the same z location and the raycast is not working. My understanding is that hit.point.y should return the hit transform of the raycast and should be a different value if the terrain under it has mountains or elevation to it correct? well i am not having much luck sticking trees to the ground.
I have the trees generating how i want them to but the y position is not correct. I cant seam to get the raycast to work more then once. All trees are added on the same y axis. which would be find if i had a flat terrain but there is mountains and a lot of trees end up below or half way in the terrain.
Also for organization i would like to add all of the trees to a empty game object. and i cant seam to add the trees to Foliagesets as the parent.
Please help.
Ive been looking at the code for the past 2 days im just confusing my self more.
Sorry i should explain how this works.
Create empty game object.
add this script on to it. 3..Add a tree prefab.
select empty game object know renamed to FoliageSets.
set amount of trees to spawn.
Set length and width to spawn trees in. (box or rectangle)
drag the object to a general area where u want trees to spawn.
using UnityEngine; using System.Collections;
public class RandomizeTrees : MonoBehaviour { public Transform tree; public Vector3 postiion; public int totalTrees = 10; public GameObject Foliageset; public float FoliageWidth; public float FoliageLength; private int FoliagePosX; private int FoliagePosZ; private int newdistance; private float heightAboveGround; // Use this for initialization void Start () { FoliagePosX = (int)Foliageset.transform.position.x; FoliagePosZ = (int)Foliageset.transform.position.z; int spawned = 0; while (spawned <= totalTrees) { int newdistance = 0; Vector3 position = new Vector3 (Random.Range (FoliagePosX , Foliageset.transform.position.x + FoliageWidth), Foliageset.transform.position.y, Random.Range (FoliagePosZ ,FoliageLength + Foliageset.transform.position.z )); RaycastHit hit = new RaycastHit(); if (Physics.Raycast(transform.localPosition,transform.TransformDirection(Vector3.down), out hit)) { newdistance = (int)hit.point.y; position = new Vector3 (Random.Range (FoliagePosX, Foliageset.transform.position.x + FoliageWidth), newdistance, Random.Range (FoliagePosZ, FoliageLength + Foliageset.transform.position.z)); tree.name = Foliageset.name + "_Tree"+spawned; //Foliageset = GameObject.Find("FoliageSets"); //tree.transform.SetParent(Foliageset); Instantiate (tree, position, Quaternion.identity); } hit = new RaycastHit(); spawned++; } } // Update is called once per frame void Update () { } }
Answer by macorig · Jul 03, 2016 at 09:23 PM
Hi! In your while loop, you fire a raycast with Physics.Raycast(...) for every tree you want to place. The problem is, that you fire it from the same position each time: transform.localPosition. The Raycast function's first argument is the ray's origin point, so here you want to use the tree's future, randomized x and z positions. The y value you will get from the hit.point, which is the point where the ray hit a collider in world space. I've updated the code for you and hope it works the way you want it to. Please note that I have tried to simplify it a bit and also got rid of the "Foliageset", since I wasn't entirely sure what it was supposed to do, but you should be able to adapt this, either way:
using UnityEngine;
using System.Collections;
public class RandomizeTrees : MonoBehaviour {
public Transform tree;
public int totalTrees = 10;
public float foliageWidth;
public float foliageLength;
// Use this for initialization
void Start () {
int spawned = 0;
while (spawned < totalTrees)
{
Vector3 position = new Vector3 (transform.position.x + Random.value * foliageWidth, transform.position.y, transform.position.z + Random.value * foliageLength);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(position, Vector3.down, out hit)) {
Transform newTree = (Transform) Instantiate (tree, hit.point, Quaternion.identity);
//newTree.transform.SetParent(transform);
newTree.name = gameObject.name + "_Tree"+spawned;
}
spawned++;
}
}
// // Update is called once per frame
// void Update () {
//
//
// }
}
Your answer
Follow this Question
Related Questions
Leaf shader for mobile 0 Answers
Terrain Tree Index Removed, But Sometimes Not And I got An Error 0 Answers
Unity tree branches and leaves become partly invisible in a distance 1 Answer
material looks pink with "Tree creator bark" shader 0 Answers
Tree Editor: Bark Textures downsize when "Optimized Bark Material" is created 0 Answers