Initial commit
This commit is contained in:
36
Assets/Scripts/PlayZone.cs
Normal file
36
Assets/Scripts/PlayZone.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayZone : MonoBehaviour
|
||||
{
|
||||
private BoxCollider _box;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_box = GetComponent<BoxCollider>();
|
||||
}
|
||||
|
||||
public Vector3 ClampToZone(Vector3 position)
|
||||
{
|
||||
Vector3 localPos = transform.InverseTransformPoint(position);
|
||||
Vector3 halfSize = _box.size / 2f;
|
||||
|
||||
localPos.x = Mathf.Clamp(localPos.x, -halfSize.x, halfSize.x);
|
||||
localPos.z = Mathf.Clamp(localPos.z, -halfSize.z, halfSize.z);
|
||||
|
||||
return transform.TransformPoint(localPos);
|
||||
}
|
||||
|
||||
public Vector3 GetRandomPoint()
|
||||
{
|
||||
Vector3 halfSize = _box.size / 2f;
|
||||
|
||||
float randomX = Random.Range(-halfSize.x, halfSize.x);
|
||||
float randomZ = Random.Range(-halfSize.z, halfSize.z);
|
||||
|
||||
Vector3 localPoint = new Vector3(randomX, 0f, randomZ);
|
||||
|
||||
return transform.TransformPoint(localPoint);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user