Quantcast
Channel: Questions in topic: "tree"
Viewing all articles
Browse latest Browse all 513

Place tree on terrain. RayCast find hit.point.y only works once.

$
0
0
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. 1. Create empty game object. 2. add this script on to it. 3..Add a tree prefab. 4. select empty game object know renamed to FoliageSets. 5. set amount of trees to spawn. 6. Set length and width to spawn trees in. (box or rectangle) 7. 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 () { } }

Viewing all articles
Browse latest Browse all 513

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>