diff --git a/Unity-Files/Assets/Examples/Defender/Pastie.prefab b/Unity-Files/Assets/Examples/Defender/Pastie.prefab index aba68b0..bfe43ed 100644 --- a/Unity-Files/Assets/Examples/Defender/Pastie.prefab +++ b/Unity-Files/Assets/Examples/Defender/Pastie.prefab @@ -12,6 +12,7 @@ GameObject: - component: {fileID: 1762312007996219834} - component: {fileID: 9035969042858155278} - component: {fileID: 2612555762601387375} + - component: {fileID: -3073797049814522211} - component: {fileID: 885697997895152444} m_Layer: 0 m_Name: Pastie @@ -125,6 +126,18 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: timeToDestruction: 10 +--- !u!114 &-3073797049814522211 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2252406525984333962} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a8a1847c1378264469a170f37d758dca, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!210 &885697997895152444 SortingGroup: m_ObjectHideFlags: 0 diff --git a/Unity-Files/Assets/Scripts/Gameplay/PastySpawn.cs b/Unity-Files/Assets/Scripts/Gameplay/PastySpawn.cs new file mode 100644 index 0000000..9c81e15 --- /dev/null +++ b/Unity-Files/Assets/Scripts/Gameplay/PastySpawn.cs @@ -0,0 +1,40 @@ +using UnityEngine; +using DG.Tweening; + +public class PastySpawn : MonoBehaviour +{ + private void Start() + { + transform.DOMove(GetNewRandomPoint(), 1); + } + + private Vector3 GetNewRandomPoint() + { + Vector3 newPoint = new Vector3(transform.position.x + Random.Range(-1, 1), transform.position.y + Random.Range(-1, 1), 0); + + if (isOutOfBounds(newPoint)) + { + Bounds mainCameraBoundsOffset = GetCameraBounds(Camera.main); + mainCameraBoundsOffset.Expand(-1f); + newPoint = mainCameraBoundsOffset.ClosestPoint(this.transform.position); + newPoint.Set(newPoint.x, newPoint.y, 0f); + } + return newPoint; + } + + private bool isOutOfBounds(Vector3 targetPosition) + { + Plane[] planes = GeometryUtility.CalculateFrustumPlanes(Camera.main); + if (GeometryUtility.TestPlanesAABB(planes, new Bounds(targetPosition, GetComponent().bounds.size))) + return false; + else + return true; + } + + private static Bounds GetCameraBounds(Camera camera) + { + float screenAspect = (float)Screen.width / (float)Screen.height; + float cameraHeight = camera.orthographicSize * 2; + return new Bounds(camera.transform.position, new Vector3(cameraHeight * screenAspect, cameraHeight, 0)); + } +} \ No newline at end of file diff --git a/Unity-Files/Assets/Scripts/Gameplay/PastySpawn.cs.meta b/Unity-Files/Assets/Scripts/Gameplay/PastySpawn.cs.meta new file mode 100644 index 0000000..5ef6b94 --- /dev/null +++ b/Unity-Files/Assets/Scripts/Gameplay/PastySpawn.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a8a1847c1378264469a170f37d758dca +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: