From 7bd34736bebcf3aff6c8109649387ad91c48f598 Mon Sep 17 00:00:00 2001 From: Christopher Justine <13519006@std.stei.itb.ac.id> Date: Sun, 10 Apr 2022 07:48:44 +0700 Subject: [PATCH] feat: add upgrade weapon option --- Assets/Prefabs/LineRenderer.prefab | 131 ++++++++++++++++++++++++ Assets/Prefabs/LineRenderer.prefab.meta | 7 ++ Assets/Scenes/Level_01.unity | 100 +----------------- Assets/Scripts/Player/PlayerShooting.cs | 92 +++++++++++++---- 4 files changed, 212 insertions(+), 118 deletions(-) create mode 100644 Assets/Prefabs/LineRenderer.prefab create mode 100644 Assets/Prefabs/LineRenderer.prefab.meta diff --git a/Assets/Prefabs/LineRenderer.prefab b/Assets/Prefabs/LineRenderer.prefab new file mode 100644 index 0000000..4051ed6 --- /dev/null +++ b/Assets/Prefabs/LineRenderer.prefab @@ -0,0 +1,131 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &3863264727155098869 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 7828756173316172361} + - component: {fileID: 756113303200918365} + m_Layer: 10 + m_Name: LineRenderer + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &7828756173316172361 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3863264727155098869} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!120 &756113303200918365 +LineRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3863264727155098869} + m_Enabled: 0 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_MotionVectors: 0 + m_LightProbeUsage: 0 + m_ReflectionProbeUsage: 0 + m_RayTracingMode: 0 + m_RayTraceProcedural: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: 007cd3dce0e6539409edcc484011c877, type: 2} + 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: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_Positions: + - {x: 0, y: 0, z: 0} + - {x: 0, y: 0, z: 1} + m_Parameters: + serializedVersion: 3 + widthMultiplier: 1 + widthCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 0.05000305 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + colorGradient: + serializedVersion: 2 + key0: {r: 1, g: 1, b: 1, a: 1} + key1: {r: 1, g: 1, b: 1, a: 1} + key2: {r: 0, g: 0, b: 0, a: 0} + key3: {r: 0, g: 0, b: 0, a: 0} + key4: {r: 0, g: 0, b: 0, a: 0} + key5: {r: 0, g: 0, b: 0, a: 0} + key6: {r: 0, g: 0, b: 0, a: 0} + key7: {r: 0, g: 0, b: 0, a: 0} + ctime0: 0 + ctime1: 65535 + ctime2: 0 + ctime3: 0 + ctime4: 0 + ctime5: 0 + ctime6: 0 + ctime7: 0 + atime0: 0 + atime1: 65535 + atime2: 0 + atime3: 0 + atime4: 0 + atime5: 0 + atime6: 0 + atime7: 0 + m_Mode: 0 + m_NumColorKeys: 2 + m_NumAlphaKeys: 2 + numCornerVertices: 0 + numCapVertices: 0 + alignment: 0 + textureMode: 0 + shadowBias: 0.5 + generateLightingData: 0 + m_UseWorldSpace: 1 + m_Loop: 0 diff --git a/Assets/Prefabs/LineRenderer.prefab.meta b/Assets/Prefabs/LineRenderer.prefab.meta new file mode 100644 index 0000000..05f1b44 --- /dev/null +++ b/Assets/Prefabs/LineRenderer.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: a9c5ba8bf0e584043bde610bffb1985c +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/Level_01.unity b/Assets/Scenes/Level_01.unity index 126cdf4..1a0bda9 100644 --- a/Assets/Scenes/Level_01.unity +++ b/Assets/Scenes/Level_01.unity @@ -1096,104 +1096,6 @@ Light: m_UseViewFrustumForShadowCasterCull: 1 m_ShadowRadius: 0 m_ShadowAngle: 0 ---- !u!120 &581701018 -LineRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 581701014} - m_Enabled: 0 - m_CastShadows: 1 - m_ReceiveShadows: 1 - m_DynamicOccludee: 1 - m_MotionVectors: 0 - m_LightProbeUsage: 0 - m_ReflectionProbeUsage: 0 - m_RayTracingMode: 0 - m_RayTraceProcedural: 0 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: - - {fileID: 2100000, guid: 007cd3dce0e6539409edcc484011c877, type: 2} - 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: 0 - m_SelectedEditorRenderState: 3 - m_MinimumChartSize: 4 - m_AutoUVMaxDistance: 0.5 - m_AutoUVMaxAngle: 89 - m_LightmapParameters: {fileID: 0} - m_SortingLayerID: 0 - m_SortingLayer: 0 - m_SortingOrder: 0 - m_Positions: - - {x: 0, y: 0, z: 0} - - {x: 0, y: 0, z: 1} - m_Parameters: - serializedVersion: 3 - widthMultiplier: 1 - widthCurve: - serializedVersion: 2 - m_Curve: - - serializedVersion: 3 - time: 0 - value: 0.05 - inSlope: 0 - outSlope: 0 - tangentMode: 0 - weightedMode: 0 - inWeight: 0.33333334 - outWeight: 0.33333334 - m_PreInfinity: 2 - m_PostInfinity: 2 - m_RotationOrder: 4 - colorGradient: - serializedVersion: 2 - key0: {r: 1, g: 1, b: 1, a: 1} - key1: {r: 1, g: 1, b: 1, a: 1} - key2: {r: 0, g: 0, b: 0, a: 0} - key3: {r: 0, g: 0, b: 0, a: 0} - key4: {r: 0, g: 0, b: 0, a: 0} - key5: {r: 0, g: 0, b: 0, a: 0} - key6: {r: 0, g: 0, b: 0, a: 0} - key7: {r: 0, g: 0, b: 0, a: 0} - ctime0: 0 - ctime1: 65535 - ctime2: 0 - ctime3: 0 - ctime4: 0 - ctime5: 0 - ctime6: 0 - ctime7: 0 - atime0: 0 - atime1: 65535 - atime2: 0 - atime3: 0 - atime4: 0 - atime5: 0 - atime6: 0 - atime7: 0 - m_Mode: 0 - m_NumColorKeys: 2 - m_NumAlphaKeys: 2 - numCornerVertices: 0 - numCapVertices: 0 - alignment: 0 - textureMode: 0 - shadowBias: 0.5 - generateLightingData: 0 - m_UseWorldSpace: 1 - m_Loop: 0 --- !u!198 &581701019 ParticleSystem: m_ObjectHideFlags: 0 @@ -6060,6 +5962,8 @@ MonoBehaviour: damagePerShot: 20 timeBetweenBullets: 0.15 range: 100 + linePrefab: {fileID: 756113303200918365, guid: a9c5ba8bf0e584043bde610bffb1985c, + type: 3} --- !u!1 &732164003 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Player/PlayerShooting.cs b/Assets/Scripts/Player/PlayerShooting.cs index 2a02861..9653846 100644 --- a/Assets/Scripts/Player/PlayerShooting.cs +++ b/Assets/Scripts/Player/PlayerShooting.cs @@ -1,17 +1,25 @@ -using UnityEngine; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; public class PlayerShooting : MonoBehaviour { public int damagePerShot = 20; public float timeBetweenBullets = 0.15f; public float range = 100f; + public LineRenderer linePrefab; float timer; Ray shootRay = new Ray(); RaycastHit shootHit; int shootableMask; ParticleSystem gunParticles; - LineRenderer gunLine; + List<LineRenderer> gunLines = new List<LineRenderer>(); + List<int> angles = new List<int>(); + int shootCount = 1; + int angle = 0; + const int maxUpgrade = 3; + int upgradeCount = 0; AudioSource gunAudio; Light gunLight; float effectsDisplayTime = 0.2f; @@ -22,11 +30,17 @@ public class PlayerShooting : MonoBehaviour { shootableMask = LayerMask.GetMask("Shootable"); gunParticles = GetComponent<ParticleSystem>(); - gunLine = GetComponent<LineRenderer>(); gunAudio = GetComponent<AudioSource>(); gunLight = GetComponent<Light>(); + gunLines.Add(Instantiate(linePrefab, transform)); + angles.Add(angle); + player = GameObject.FindWithTag("Player"); + UpgradeWeapon(0); + UpgradeWeapon(0); + UpgradeWeapon(1); + UpgradeWeapon(0); } void Update() @@ -46,10 +60,43 @@ public class PlayerShooting : MonoBehaviour public void DisableEffects() { - gunLine.enabled = false; + foreach (var line in gunLines) + { + line.enabled = false; + } gunLight.enabled = false; } + public void UpgradeWeapon(int option) + { + if (upgradeCount >= maxUpgrade) + { + return; + } + + upgradeCount++; + if (option == 0) + { + ShootCountUpgrade(); + return; + } + ShootSpeedUpgrade(); + } + + private void ShootCountUpgrade() + { + gunLines.Add(Instantiate(linePrefab, transform)); + gunLines.Add(Instantiate(linePrefab, transform)); + angle += 10; + angles.Add(angle); + angles.Add(angle * -1); + } + + private void ShootSpeedUpgrade() + { + timeBetweenBullets -= 0.03f; + } + public void Shoot() { timer = 0f; @@ -61,27 +108,32 @@ public class PlayerShooting : MonoBehaviour gunParticles.Stop(); gunParticles.Play(); - gunLine.enabled = true; - gunLine.SetPosition(0, transform.position); + int idx = 0; + foreach (var line in gunLines) + { + line.enabled = true; + line.SetPosition(0, transform.position); - shootRay.origin = transform.position; - shootRay.direction = transform.forward; + shootRay.origin = transform.position; + shootRay.direction = Quaternion.Euler(0, angles.ElementAt(idx), 0) * transform.forward; - if (Physics.Raycast(shootRay, out shootHit, range, shootableMask)) - { - EnemyHealth enemyHealth = shootHit.collider.GetComponent<EnemyHealth>(); + if (Physics.Raycast(shootRay, out shootHit, range, shootableMask)) + { + EnemyHealth enemyHealth = shootHit.collider.GetComponent<EnemyHealth>(); + + if (enemyHealth != null) + { + PlayerPower playerPower = player.GetComponent<PlayerPower>(); + enemyHealth.TakeDamage(playerPower.currentPower, shootHit.point); + } - if (enemyHealth != null) + line.SetPosition(1, shootHit.point); + } + else { - PlayerPower playerPower = player.GetComponent<PlayerPower>(); - enemyHealth.TakeDamage(playerPower.currentPower, shootHit.point); + line.SetPosition(1, shootRay.origin + shootRay.direction * range); } - - gunLine.SetPosition(1, shootHit.point); - } - else - { - gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range); + idx++; } } } \ No newline at end of file -- GitLab