Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why min distance not working?

Discussion in 'Scripting' started by polan31, Nov 4, 2018.

  1. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    I have a "Gear" object that spawns.

    I have determined the minimum and max distance that can be between objects.

    Hhowever, objects still spawn on other objects, covering (overlapping) them.

    How to change it?

    My script:


    Code (CSharp):
    1.  using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GearsSpawner : MonoBehaviour {
    6.  
    7.      public GameObject theGear;
    8.      public Transform generationPoint;
    9.  
    10.      public float distanceBetween;
    11.      public float distanceBetweenMin;
    12.      public float distanceBetweenMax;
    13.  
    14.      private int gearSelector;
    15.      public GameObject[] theGears;
    16.  
    17.      private float minWidth;
    18.      public Transform maxWidthPoint;
    19.  
    20.      private float maxWidth;
    21.      public float maxWidthChange;
    22.      private float widthChange;
    23.  
    24.      void Start (){
    25.          minWidth = transform.position.x;
    26.          maxWidth = maxWidthPoint.position.x;
    27.      }
    28.        
    29.    
    30.      void Update (){
    31.          if (transform.position.y < generationPoint.position.y)
    32.          {
    33.              distanceBetween = Random.Range (distanceBetweenMin, distanceBetweenMax);
    34.              gearSelector = Random.Range (0, theGears.Length);
    35.              widthChange = transform.position.x + Random.Range (maxWidthChange, -maxWidthChange);
    36.              if (widthChange > maxWidth) {
    37.                  widthChange = maxWidth;
    38.              } else if (widthChange < minWidth)
    39.              {
    40.                  widthChange = minWidth;
    41.              }
    42.              transform.position = new Vector3 (widthChange, transform.position.y + distanceBetween, transform.position.z);
    43.              Instantiate (theGears[gearSelector], transform.position, transform.rotation);
    44.          }
    45. }
    46. }
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    You are most likely have error in script.
    I suggest you to use debugger breakpoint, to track down the error.
    Is good exercise, if you haven't used it yet.
    Also you can use Debug.Log ( "some text" ), to print into console.
     
  3. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    I checked in the first place.
    There is no error.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    I don't mean actual error lie in console / script editor, but to track the bug in the code.
    Running code in steps, will allow you to watch variables.
    Watchdog is useful here.