Search Unity

Resolved Script raycast not working properly

Discussion in 'Scripting' started by cb12438, Feb 2, 2023.

  1. cb12438

    cb12438

    Joined:
    Jul 1, 2021
    Posts:
    7
    When i run this script the ray cst only wants to go down 1F and when the ray cast touches the layerMask nothing gets placed went over the values they are all fine maid another script that was nothing like this one and that one worked fine it just placed 1 object though i just cant seem to brake down the error here?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ObjectGeneration : MonoBehaviour
    6. {
    7.     public GameObject[] prefabs;
    8.  
    9.     [Header("Raycast settings")]
    10.     public int count;
    11.     public LayerMask whatIsGround;
    12.     // public int rayCastLength;
    13.  
    14.     [Space]
    15.  
    16.     public float minHeight;
    17.     public float maxHeight;
    18.     public Vector2 xRange;
    19.     public Vector2 zRange;
    20.  
    21.     [Header("Prefab Veration Settings")]
    22.     [Range(0,1)]  public float rotateTowardsNormal;
    23.     public Vector2 rotationRange;
    24.     public Vector3 minScale;
    25.     public Vector3 maxScale;
    26.     public Transform prefabParent;
    27.  
    28.     List<GameObject> prefabArray = new List<GameObject>();
    29.  
    30.     private void Awake(){
    31.         InstansiatePrefabs(new List<GameObject>(prefabs), count, maxHeight, maxHeight, minScale, maxScale);
    32.     }
    33.  
    34.     public void InstansiatePrefabs(List<GameObject> prefabs, int count, float minHeight, float maxHeight, Vector3 minScale, Vector3 maxScale){
    35.  
    36.         Debug.Log("Start of InstansiatePrefabs");
    37.  
    38.         for (int i = 0; i < count; i++)
    39.         {
    40.             Debug.Log("Iteration: " + i);
    41.  
    42.             int prefabIndex = Random.Range(0, prefabs.Count);
    43.             GameObject prefab = prefabs[prefabIndex];
    44.  
    45.             float sampleX = Random.Range(xRange.x, xRange.y);
    46.             float sampleY = Random.Range(zRange.x, zRange.y);
    47.  
    48.             Vector3 rayStart = new Vector3(sampleX, maxHeight, sampleY);
    49.  
    50.             RaycastHit hit;
    51.             if(Physics.Raycast(rayStart, Vector3.down, out hit, 1000f, whatIsGround)){
    52.                
    53.                 Debug.Log("Ray Start: " + rayStart);
    54.                 Debug.Log("Hit Point: " + hit.point);
    55.                 Debug.Log("Normal: " + hit.normal);
    56.  
    57.                 if(hit.distance >= 1000f && hit.point.y >= minHeight){
    58.                     GameObject tree = Instantiate(prefab, hit.point, Quaternion.identity);
    59.  
    60.                     tree.transform.SetParent(prefabParent);
    61.                     tree.transform.Rotate(Vector3.up, Random.Range(0, 360), Space.Self);
    62.                     tree.transform.rotation = Quaternion.Lerp(transform.rotation, transform.rotation*Quaternion.FromToRotation(tree.transform.up, hit.normal), rotateTowardsNormal);
    63.                     tree.transform.localScale = new Vector3(
    64.                     Random.Range(minScale.x, maxScale.x),
    65.                     Random.Range(minScale.y, maxScale.y),
    66.                     Random.Range(minScale.z, maxScale.z)
    67.                     );
    68.  
    69.                     prefabArray.Add(tree);
    70.                 }
    71.             }
    72.             Debug.DrawRay(rayStart, Vector3.down, Color.black, 1000);
    73.         }
    74.         Debug.Log("End of InstansiatePrefabs");
    75.     }
    76. }
    77.  
     
  2. tomfulghum

    tomfulghum

    Joined:
    May 8, 2017
    Posts:
    78
    In your Physics.Raycast call you set the maxDistance parameter to 1000f. Below, you only spawn your tree if the hit distance is greater or equal to 1000f, meaning your tree will only get planted if the raycast hit's distance is exactly 1000f. You can remove the hit distance check since maxDistance already ensures nothing further away than 1000 units will be hit.