Bug Fix Pasty Spawn Out of Bounds

This commit is contained in:
Philip W 2021-11-15 16:48:04 +00:00
parent 36883be734
commit 69f57d3000

View File

@ -3,8 +3,12 @@ using DG.Tweening;
public class PastySpawn : MonoBehaviour public class PastySpawn : MonoBehaviour
{ {
private float cameraBoundsOffset = -3f;
private Bounds mainCameraBounds;
private void Start() private void Start()
{ {
mainCameraBounds = GetCameraBounds(Camera.main);
mainCameraBounds.Expand(cameraBoundsOffset);
transform.DOMove(GetNewRandomPoint(), 1); transform.DOMove(GetNewRandomPoint(), 1);
} }
@ -14,9 +18,7 @@ public class PastySpawn : MonoBehaviour
if (isOutOfBounds(newPoint)) if (isOutOfBounds(newPoint))
{ {
Bounds mainCameraBoundsOffset = GetCameraBounds(Camera.main); newPoint = mainCameraBounds.ClosestPoint(newPoint);
mainCameraBoundsOffset.Expand(-1f);
newPoint = mainCameraBoundsOffset.ClosestPoint(this.transform.position);
newPoint.Set(newPoint.x, newPoint.y, 0f); newPoint.Set(newPoint.x, newPoint.y, 0f);
} }
return newPoint; return newPoint;
@ -24,11 +26,7 @@ public class PastySpawn : MonoBehaviour
private bool isOutOfBounds(Vector3 targetPosition) private bool isOutOfBounds(Vector3 targetPosition)
{ {
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(Camera.main); return mainCameraBounds.Intersects(new Bounds(targetPosition, GetComponent<BoxCollider2D>().bounds.size)) ? false : true;
if (GeometryUtility.TestPlanesAABB(planes, new Bounds(targetPosition, GetComponent<BoxCollider2D>().bounds.size)))
return false;
else
return true;
} }
private static Bounds GetCameraBounds(Camera camera) private static Bounds GetCameraBounds(Camera camera)