-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomPosition2D.cs
35 lines (29 loc) · 977 Bytes
/
RandomPosition2D.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using UnityEngine;
public class RandomPosition2D : MonoBehaviour
{
[SerializeField]
private Color colorOfAreaInEditor = new Color(0.5f, 0.5f, 0.5f, 0.2f);
private float xRange = 10f, yRange = 10f;
private float OffSetValue(float valueToOffSet, float range) => valueToOffSet + Random.Range(-range, range);
private void OnDrawGizmos()
{
Gizmos.color = colorOfAreaInEditor;
Gizmos.DrawCube(transform.position, transform.localScale);
}
private void Awake()
{
InitializeRangeValues();
}
private void InitializeRangeValues()
{
xRange = transform.localScale.x / 2f;
yRange = transform.localScale.y / 2f;
}
///<summary>
///Will choose a random position inside this area (X, Y).
///</summary>
public Vector2 RandomLocationInsideArea()
{
return new Vector3(OffSetValue(transform.position.x, xRange), OffSetValue(transform.position.y, yRange), 0);
}
}