Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Random.Range Raycast - Not Random

Discussion in 'Scripting' started by RussellT, Feb 25, 2015.

  1. RussellT

    RussellT

    Joined:
    Feb 1, 2015
    Posts:
    18
    Hello All,

    I am trying to fire a ray randomly between the ranges set out from my position variable. Further down my code I then check the Pixel Value on the hit and place it if the value matches one I have defined.

    My issue is that although it all works, I seem to get very defined areas where the ray hits/Cube is created. Attached is an image of the pattern I am getting.

    Below just a snippet of the code with the main RayCast part!

    Code (JavaScript):
    1.  
    2. var position : Vector3;
    3. var hit : RaycastHit;
    4. var down = Vector3(0,-100, 0);
    5.  
    6.  
    7.  
    8. function Update ()
    9. {
    10.      position = new Vector3(Random.Range(-5, 5.0F),Random.Range(10,15), Random.Range(-5, 5));
    11.     if (Physics.Raycast (position, down, hit))
    12. {
    13.  
    14. }
    15. }
    16.  
    Thanks in advance,
    Russell
     

    Attached Files:

  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,999
    if you make them all floats, seems like bit more variation:
    Code (JavaScript):
    1.      position = new Vector3(Random.Range(-5f, 5.0F),Random.Range(10f,15f), Random.Range(-5f, 5f));
     
  3. RussellT

    RussellT

    Joined:
    Feb 1, 2015
    Posts:
    18
    What a fool! Works perfectly, Many thanks :)