diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 5700998..b9ba583 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -204,38 +204,6 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!1 &620680906 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 620680907} - m_Layer: 0 - m_Name: Laser Rail - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!4 &620680907 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 620680906} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 1.1082339, y: -0.09741877, z: -2.1782916} - m_LocalScale: {x: 1, y: 1, z: 1} - m_Children: - - {fileID: 934827305} - - {fileID: 1748837247} - m_Father: {fileID: 0} - m_RootOrder: 1 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &934827303 GameObject: m_ObjectHideFlags: 0 @@ -250,7 +218,7 @@ GameObject: - component: {fileID: 934827306} - component: {fileID: 934827308} m_Layer: 0 - m_Name: Turret + m_Name: Laser Turret m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -314,11 +282,11 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 934827303} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} - m_LocalPosition: {x: -11.29, y: 0.09741877, z: 2.1782916} + m_LocalPosition: {x: -10.01, y: -0.02, z: 0} m_LocalScale: {x: 0.5, y: 0.5, z: 1} m_Children: [] - m_Father: {fileID: 620680907} - m_RootOrder: 0 + m_Father: {fileID: 0} + m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &934827306 MonoBehaviour: @@ -333,6 +301,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: speed: 10 + distance: 3 isHorizontal: 0 --- !u!120 &934827307 LineRenderer: @@ -374,8 +343,8 @@ LineRenderer: m_SortingLayer: 0 m_SortingOrder: 0 m_Positions: - - {x: -10, y: 0, z: 0} - - {x: 1, y: 0, z: 0} + - {x: 0, y: 0, z: 0} + - {x: 0, y: 0, z: 0} m_Parameters: serializedVersion: 3 widthMultiplier: 0.1 @@ -443,6 +412,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: d3775ebae34c5ac44af73819982f2b40, type: 3} m_Name: m_EditorClassIdentifier: + flipDirection: 0 --- !u!1 &1320437470 GameObject: m_ObjectHideFlags: 0 @@ -654,84 +624,3 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 90} ---- !u!1 &1748837245 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 1748837247} - - component: {fileID: 1748837246} - m_Layer: 0 - m_Name: Rail - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!212 &1748837246 -SpriteRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1748837245} - m_Enabled: 1 - m_CastShadows: 0 - m_ReceiveShadows: 0 - m_DynamicOccludee: 1 - m_MotionVectors: 1 - m_LightProbeUsage: 1 - m_ReflectionProbeUsage: 1 - m_RayTracingMode: 0 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: - - {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} - m_StaticBatchInfo: - firstSubMesh: 0 - subMeshCount: 0 - m_StaticBatchRoot: {fileID: 0} - m_ProbeAnchor: {fileID: 0} - m_LightProbeVolumeOverride: {fileID: 0} - m_ScaleInLightmap: 1 - m_ReceiveGI: 1 - m_PreserveUVs: 0 - m_IgnoreNormalsForChartDetection: 0 - m_ImportantGI: 0 - m_StitchLightmapSeams: 1 - m_SelectedEditorRenderState: 0 - m_MinimumChartSize: 4 - m_AutoUVMaxDistance: 0.5 - m_AutoUVMaxAngle: 89 - m_LightmapParameters: {fileID: 0} - m_SortingLayerID: 0 - m_SortingLayer: 0 - m_SortingOrder: 0 - m_Sprite: {fileID: 21300000, guid: 85e455eb0e01f3b4b97dec1065971269, type: 3} - m_Color: {r: 0.5188679, g: 0.5188679, b: 0.5188679, a: 1} - m_FlipX: 0 - m_FlipY: 0 - m_DrawMode: 0 - m_Size: {x: 1, y: 1} - m_AdaptiveModeThreshold: 0.5 - m_SpriteTileMode: 0 - m_WasSpriteAssigned: 1 - m_MaskInteraction: 0 - m_SpriteSortPoint: 0 ---- !u!4 &1748837247 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1748837245} - m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} - m_LocalPosition: {x: -11.598234, y: 0.09941877, z: 2.1782916} - m_LocalScale: {x: 0.29290247, y: 9.713962, z: 1} - m_Children: [] - m_Father: {fileID: 620680907} - m_RootOrder: 1 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} diff --git a/Assets/Scripts/Laser.cs b/Assets/Scripts/Laser.cs index db1af6c..4b222cc 100644 --- a/Assets/Scripts/Laser.cs +++ b/Assets/Scripts/Laser.cs @@ -4,21 +4,36 @@ using UnityEngine; public class Laser : MonoBehaviour { - float maxDistance = 500.0f; + public bool flipDirection; + float maxDistance = 1000.0f; + bool isHorizontal; LineRenderer laserLine; void Start() { - laserLine = GetComponent (); + laserLine = GetComponent(); + isHorizontal = GetComponent().isHorizontal; } void Update() { - RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right); - float endpointX = maxDistance; - if (hit.collider) endpointX = hit.collider.transform.position.x; - laserLine.SetPosition (0, new Vector3(transform.position.x, transform.position.y, 0)); - laserLine.SetPosition (1, new Vector3(endpointX, transform.position.y, 0)); + laserLine.SetPosition(0, transform.position); + + if (isHorizontal) + { + float endpointY = flipDirection ? transform.position.y - maxDistance : transform.position.y + maxDistance; + RaycastHit2D hit = Physics2D.Raycast(transform.position, flipDirection ? Vector2.down : Vector2.up); + + if (hit.collider) endpointY = hit.collider.transform.position.y; + laserLine.SetPosition(1, new Vector3(transform.position.x, endpointY, 0)); + } else + { + float endpointX = flipDirection ? transform.position.x - maxDistance : transform.position.x + maxDistance; + RaycastHit2D hit = Physics2D.Raycast(transform.position, flipDirection ? Vector2.left : Vector2.right); + + if (hit.collider) endpointX = hit.collider.transform.position.x; + laserLine.SetPosition(1, new Vector3(endpointX, transform.position.y, 0)); + } } } diff --git a/Assets/Scripts/Turret.cs b/Assets/Scripts/Turret.cs index 080800f..c8cf66b 100644 --- a/Assets/Scripts/Turret.cs +++ b/Assets/Scripts/Turret.cs @@ -4,12 +4,38 @@ using UnityEngine; public class Turret : MonoBehaviour { - public float speed = 10; + public float speed = 10.0f; + public float distance = 2.0f; public bool isHorizontal = false; + float startX; + float startY; + + void Start() + { + startX = transform.position.x; + startY = transform.position.y; + } + void Update() { - transform.position += getMovementVector() * speed * Time.deltaTime; + Vector3 movementVector = getMovementVector(); + + if (allowMovement(movementVector)) moveTurret(movementVector); + } + + bool allowMovement(Vector3 movementVector) + { + bool moveUp = movementVector.y > 0; + bool moveDown = movementVector.y < 0; + bool moveRight = movementVector.x > 0; + bool moveLeft = movementVector.x < 0; + + if (moveUp && transform.position.y >= startY + distance) return false; + if (moveDown && transform.position.y <= startY - distance) return false; + if (moveRight && transform.position.x >= startX + distance) return false; + if (moveLeft && transform.position.x <= startX - distance) return false; + return true; } Vector3 getMovementVector() @@ -19,4 +45,17 @@ public class Turret : MonoBehaviour return new Vector3(x, y, 0); } + + void moveTurret(Vector3 movementVector) + { + transform.position += movementVector * speed * Time.deltaTime; + } + + void OnDrawGizmosSelected() + { + Vector3 offset = isHorizontal ? new Vector3(distance, 0, 0) : new Vector3(0, distance, 0); + + Gizmos.color = Color.red; + Gizmos.DrawLine(transform.position - offset, transform.position + offset); + } }