Added Pastie Spawn Script With Bounds Check
This commit is contained in:
		
							parent
							
								
									5820b9c9bd
								
							
						
					
					
						commit
						c4f9a40659
					
				
							
								
								
									
										13
									
								
								Unity-Files/Assets/Examples/Defender/Pastie.prefab
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										13
									
								
								Unity-Files/Assets/Examples/Defender/Pastie.prefab
									
									
									
										generated
									
									
									
								
							| @ -12,6 +12,7 @@ GameObject: | |||||||
|   - component: {fileID: 1762312007996219834} |   - component: {fileID: 1762312007996219834} | ||||||
|   - component: {fileID: 9035969042858155278} |   - component: {fileID: 9035969042858155278} | ||||||
|   - component: {fileID: 2612555762601387375} |   - component: {fileID: 2612555762601387375} | ||||||
|  |   - component: {fileID: -3073797049814522211} | ||||||
|   - component: {fileID: 885697997895152444} |   - component: {fileID: 885697997895152444} | ||||||
|   m_Layer: 0 |   m_Layer: 0 | ||||||
|   m_Name: Pastie |   m_Name: Pastie | ||||||
| @ -125,6 +126,18 @@ MonoBehaviour: | |||||||
|   m_Name:  |   m_Name:  | ||||||
|   m_EditorClassIdentifier:  |   m_EditorClassIdentifier:  | ||||||
|   timeToDestruction: 10 |   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 | --- !u!210 &885697997895152444 | ||||||
| SortingGroup: | SortingGroup: | ||||||
|   m_ObjectHideFlags: 0 |   m_ObjectHideFlags: 0 | ||||||
|  | |||||||
							
								
								
									
										40
									
								
								Unity-Files/Assets/Scripts/Gameplay/PastySpawn.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								Unity-Files/Assets/Scripts/Gameplay/PastySpawn.cs
									
									
									
									
									
										Normal file
									
								
							| @ -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<BoxCollider2D>().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)); | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										11
									
								
								Unity-Files/Assets/Scripts/Gameplay/PastySpawn.cs.meta
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Unity-Files/Assets/Scripts/Gameplay/PastySpawn.cs.meta
									
									
									
										generated
									
									
									
										Normal file
									
								
							| @ -0,0 +1,11 @@ | |||||||
|  | fileFormatVersion: 2 | ||||||
|  | guid: a8a1847c1378264469a170f37d758dca | ||||||
|  | MonoImporter: | ||||||
|  |   externalObjects: {} | ||||||
|  |   serializedVersion: 2 | ||||||
|  |   defaultReferences: [] | ||||||
|  |   executionOrder: 0 | ||||||
|  |   icon: {instanceID: 0} | ||||||
|  |   userData:  | ||||||
|  |   assetBundleName:  | ||||||
|  |   assetBundleVariant:  | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user