UnityEngine.Random.Range C# (CSharp) Method

Range() public static method

public static Range ( float min, float max ) : float
min float
max float
return float
		public static float Range(float min, float max)
		{
			return (float)(min + random.NextDouble() * (max - min));
		}

Same methods

Random::Range ( int min, int max ) : int

Usage Example

コード例 #1
0
    void PlaceObstacles(int _probability, Dictionary <int, int> cells)
    {
        var freeCells = new List <int>();

        foreach (KeyValuePair <int, int> c in cells)
        {
            if (c.Value == 0)
            {
                freeCells.Add(c.Key);
            }
        }
        freeCells.Sort();
        for (int i = 0; i < freeCells.Count; i++)
        {
            int x = freeCells[i];
            if (Random.Range(0, 100) <= _probability)
            {
                ObstacleData _obstacle       = ObstacleDatas[Random.Range(0, ObstacleDatas.Count)];
                int          y               = GetTopGroundIndex(x);
                Vector2      pos             = Grid.CellToWorld(new Vector3Int(x, y, 0));
                float        constraintLeft  = pos.x;
                float        constraintRight = pos.x + Grid.cellSize.x;
                float        rndScale        = Random.Range(_obstacle.ScaleLimits.x, _obstacle.ScaleLimits.y);
                float        _newXsize       = _obstacle.Prefab.GetComponent <Renderer>().bounds.size.x *rndScale;
                float        _yShift;
                if (_obstacle.Prefab.name == "Bush")
                {
                    _yShift = _obstacle.Prefab.GetComponent <Renderer>().bounds.size.y *rndScale * 0.385f;
                }
                else
                {
                    _yShift = _obstacle.Prefab.GetComponent <Renderer>().bounds.size.y *rndScale * 0.5f;
                }
                pos += new Vector2(0, (Grid.cellSize.y * 0.95f) + _yShift);
                int leftConstraint = 0;
                if (Tilemap.GetTile(new Vector3Int(x - 1, y + 1, 0)) != null | Tilemap.GetTile(new Vector3Int(x - 1, y, 0)) == null)
                {
                    leftConstraint = 1;
                }
                pos.x += Random.Range(0, 1f) * Grid.cellSize.x + leftConstraint * (_newXsize * 0.5f);

                if (_previousObstacle != null)
                {
                    float minimalDistance = _previousObstacle.transform.position.x + (_previousObstacle.GetComponent <Renderer>().bounds.size.x * 0.5f) + (_newXsize * 0.5f) + ObstacleDelta;
                    if (pos.x < minimalDistance)
                    {
                        pos.x = minimalDistance;
                    }
                }
                pos = new Vector2(Mathf.Clamp(pos.x, constraintLeft, constraintRight), pos.y);
                Vector3Int _adjCell = Grid.WorldToCell(new Vector3(pos.x + (_newXsize / 2f), pos.y));
                if (Tilemap.GetTile(new Vector3Int(_adjCell.x, _adjCell.y - 1, _adjCell.z)) != null && Tilemap.GetTile(_adjCell) == null)
                {
                    _previousObstacle = _obstacle.Pool.Get().gameObject;
                    _previousObstacle.transform.SetParent(Obstacles);
                    _previousObstacle.transform.position   = pos;
                    _previousObstacle.transform.localScale = new Vector3(rndScale, rndScale, rndScale);
                    if (Random.Range(0, 2) == 0)
                    {
                        _previousObstacle.transform.Rotate(new Vector3(0, 180, 0));
                    }
                    cells[x] = 3;
                }
            }
        }
    }
All Usage Examples Of UnityEngine.Random::Range
Random