-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomPosition3D.cs
47 lines (40 loc) · 1.35 KB
/
RandomPosition3D.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
36
37
38
39
40
41
42
43
44
45
46
47
using UnityEngine;
public class RandomPosition3D : MonoBehaviour
{
[SerializeField]
private Color GizmosColor = new Color(0.5f, 0.5f, 0.5f, 0.2f);
private float xRange = 10f, yRange = 10f, zRange = 10f;
private float Range(float scale, float range) => scale + Random.Range(-range, range);
private void OnDrawGizmos()
{
Gizmos.color = GizmosColor;
Gizmos.DrawCube(transform.position, transform.localScale);
}
private void Awake()
{
Initialize();
}
//Gets stats from localscale of your spawn area
private void Initialize()
{
xRange = transform.localScale.x / 2f;
yRange = transform.localScale.y / 2f;
zRange = transform.localScale.z / 2f;
}
///<summary>
///Will choose a random position inside area choosing a random X, Z and with fixed Y value.
///</summary>
public Vector3 SpawnPositionCustomHeight(float customY = 0)
{
var position = transform.position;
return new Vector3(Range(position.x, xRange), customY, Range(position.z, zRange));
}
///<summary>
///Will choose a random position in X, Y Z.
///</summary>
public Vector3 SpawnPosition()
{
var position = transform.position;
return new Vector3(Range(position.x, xRange), Range(position.y, yRange), Range(position.z, zRange));;
}
}