Skip to content

Instantly share code, notes, and snippets.

@smkplus
Last active November 26, 2019 10:21
Show Gist options
  • Save smkplus/b449220ab1f485c8df03cf78503ac44f to your computer and use it in GitHub Desktop.
Save smkplus/b449220ab1f485c8df03cf78503ac44f to your computer and use it in GitHub Desktop.
Random Inside RectTransform

Record_2019_11_26_13_42_34_194

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

public class RandomPointInsideRectangle : MonoBehaviour
{
    public GameObject prefab;
    
    private void Update()
    {  
        var scale = GetComponent<RectTransform>().rect.size / 2f ;
        var x = Random.Range(-scale.x, scale.x);
        var y = Random.Range(-scale.y, scale.y);
       var spawn = Instantiate(prefab, transform);
       spawn.transform.localPosition = new Vector3(x, y, 0);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment