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 on the X Axis

Discussion in 'Scripting' started by DLGScript, Aug 6, 2016.

  1. DLGScript

    DLGScript

    Joined:
    Jul 15, 2012
    Posts:
    272
    EDIT: Thanks guy you helped me a lot.
    By the way maybe you can help me in my other thread:
    http://forum.unity3d.com/threads/random-number-only-once.424469/
    -------------------------------------------------------------------
    Hey guys!
    This is my code:
    clickButton.GetComponent<RectTransform> ().localPosition = new Vector3 ((Random.Range (-12.0F, 12.0F)) * 0.5f, Random.Range (-10.0F, 10.0F), clickButton.position.z);

    This is what im trying to do:
    I have an UI image that i want randomally move on my screen everytime i click on it.
    Right now what I have is that The image moves on the X Axis between 12 to -12, and on the Y Axis between 10 to -10.

    I want to change that, I want to move the image like in the image and not like I explained to you earlier..
    I want to move the image on the X Axis between 6 to 12 and between -6 to -12.
    For an example:
    Random.Range (-6.0F, -12.0F) AND Random.Range (6.0F, 12.0F) BOT IN THE SAME X aXIS SCRIPT.
    This is an image for demonstration:


    Thanks in advance
     
    Last edited: Aug 6, 2016
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    I think you can just randomize the sign and get the results you want.

    Pseudocode:
    Code (csharp):
    1. float x = Random.Range(6.0f, 12.0f);
    2. x *= Random.Range(0, 2) == 0 ? -1 : 1;
     
  3. DLGScript

    DLGScript

    Joined:
    Jul 15, 2012
    Posts:
    272
    Can you please explain me this cause i didn't get anything from this line
     
  4. Mothil

    Mothil

    Joined:
    Jan 14, 2014
    Posts:
    434
    There's bound to be better ways to write this, but could this work?

    Code (CSharp):
    1.     private float x;
    2.  
    3.     void ClickOnImage() {
    4.         int posOrNegValue = Random.Range(0,2);
    5.  
    6.         if (posOrNegValue == 0){
    7.             x = Random.Range(6f, 12f);
    8.         }
    9.         if (posOrNegValue == 1) {
    10.             x = Random.Range(-6f, -12f);
    11.         }
    12.  
    13.         transform.position = new Vector3(x, 0, 0);
    14.     }
    This way, you will get either 0 or 1 everytime you click. If you get 0, you random on the positive value, and if you get 1, you random on the negative value.
     
    DLGScript likes this.
  5. DLGScript

    DLGScript

    Joined:
    Jul 15, 2012
    Posts:
    272
    Thanks i will give it a try man :)
     
  6. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    It's a ternary operator. It will roll 0 or 1 and compare it to 0. If the result is 0 it will multiply x by -1 otherwise it will multiply x by 1. The result will be a range from -6 to -12 or 6 to 12.

    A slightly better way to write Mothil's solution:
    Code (csharp):
    1. float x = Random.Range(0, 2) == 0 ? Random.Range(-6.0f, -12.0f) : Random.Range(6.0f, 12.0f);
     
    Last edited: Aug 6, 2016
    Mothil likes this.
  7. DLGScript

    DLGScript

    Joined:
    Jul 15, 2012
    Posts:
    272
    Your code works perfectlly :)!
     
    Mothil likes this.
  8. DLGScript

    DLGScript

    Joined:
    Jul 15, 2012
    Posts:
    272