From bb9de20d39490ed17d2614e2bfebca562fdc5dc1 Mon Sep 17 00:00:00 2001 From: AryPratama Date: Mon, 10 Jun 2024 14:30:26 +0700 Subject: [PATCH] Script Script untuk objek Bola, Bot dan UI Settings --- Bola.cs | 105 ++++++++++++++++++++++++++++++++++++++++++++++++ SettingsMenu.cs | 72 +++++++++++++++++++++++++++++++++ botEasy.cs | 56 ++++++++++++++++++++++++++ botHard.cs | 56 ++++++++++++++++++++++++++ botMedium.cs | 56 ++++++++++++++++++++++++++ 5 files changed, 345 insertions(+) create mode 100644 Bola.cs create mode 100644 SettingsMenu.cs create mode 100644 botEasy.cs create mode 100644 botHard.cs create mode 100644 botMedium.cs diff --git a/Bola.cs b/Bola.cs new file mode 100644 index 0000000..cbd1360 --- /dev/null +++ b/Bola.cs @@ -0,0 +1,105 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Bola : MonoBehaviour +{ + public GameController gameController; + public GerakanRaket playerRacket; + public AudioClip hitMejaSoundClip; + public float volume = 4.0f; + Vector3 initialPos; + bool lastHitPlayerMeja = false; + bool lastHitBotMeja = false; + + void Start() + { + initialPos = transform.position; + GetComponent().useGravity = false; + } + + private void OnCollisionEnter(Collision collision) +{ + if (collision.gameObject.CompareTag("PlayerMeja")) + { + lastHitPlayerMeja = true; + lastHitBotMeja = false; + if (hitMejaSoundClip != null) + { + AudioSource.PlayClipAtPoint(hitMejaSoundClip, transform.position, volume); + } + } + else if (collision.gameObject.CompareTag("BotMeja")) + { + lastHitBotMeja = true; + lastHitPlayerMeja = false; + if (hitMejaSoundClip != null) + { + AudioSource.PlayClipAtPoint(hitMejaSoundClip, transform.position, volume); + } + } + else if (collision.gameObject.CompareTag("Out")) + { + if (lastHitPlayerMeja) + { + gameController.AddScore("Bot", 1); + ResetBolaPosition(8.1f); + } + else if (lastHitBotMeja) + { + gameController.AddScore("Player", 1); + ResetBolaPosition(-4f); + } + else + { + ResetBolaPosition(initialPos.z); + } + + if (playerRacket != null) + { + playerRacket.ResetPlayerPosition(); + } + + ResetBots(); + + lastHitPlayerMeja = false; + lastHitBotMeja = false; + } + else if (collision.transform.CompareTag("Player")) + { + gameController.SetLastHitter("Player"); + } + else if (collision.transform.CompareTag("Bot")) + { + gameController.SetLastHitter("Bot"); + } +} + + public void ResetBola() + { + transform.position = initialPos; + GetComponent().velocity = Vector3.zero; + GetComponent().useGravity = false; + lastHitPlayerMeja = false; + lastHitBotMeja = false; + } + + void ResetBolaPosition(float zPosition) + { + Vector3 newPosition = new Vector3(0.86f, 5.65f, zPosition); + transform.position = newPosition; + + GetComponent().velocity = Vector3.zero; + GetComponent().useGravity = false; + } + + void ResetBots() + { + GameObject[] bots = GameObject.FindGameObjectsWithTag("Bot"); + foreach (GameObject bot in bots) + { + // Reset posisi bot + bot.transform.position = new Vector3(0.85251f, 5.428f, 8.41f); + } + } +} \ No newline at end of file diff --git a/SettingsMenu.cs b/SettingsMenu.cs new file mode 100644 index 0000000..d800452 --- /dev/null +++ b/SettingsMenu.cs @@ -0,0 +1,72 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Audio; +using TMPro; + +public class SettingsMenu : MonoBehaviour +{ + public AudioMixer audioMixer; + + public TMP_Dropdown qualityDropdown; + public TMP_Dropdown resolutionDropdown; + + Resolution[] resolutions; + + void Start() + { + resolutions = Screen.resolutions; + + resolutionDropdown.ClearOptions(); + qualityDropdown.ClearOptions(); + + List resolutionOptions = new List(); + List qualityOptions = new List(); + + int currentResolutionIndex = 0; + + for (int i = 0; i < resolutions.Length; i++) + { + string resolutionOption = resolutions[i].width + " x " + resolutions[i].height; + resolutionOptions.Add(resolutionOption); + + if (resolutions[i].width == Screen.currentResolution.width && + resolutions[i].height == Screen.currentResolution.height) + { + currentResolutionIndex = i; + } + } + + resolutionDropdown.AddOptions(resolutionOptions); + resolutionDropdown.value = currentResolutionIndex; + resolutionDropdown.RefreshShownValue(); + + for (int i = 0; i < QualitySettings.names.Length; i++) + { + qualityOptions.Add(QualitySettings.names[i]); + } + + qualityDropdown.AddOptions(qualityOptions); + } + + public void SetResolution(int resolutionIndex) + { + Resolution resolution = resolutions[resolutionIndex]; + Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen); + } + + public void SetVolume(float volume) + { + audioMixer.SetFloat("Volume", volume); + } + + public void SetQuality(int qualityIndex) + { + QualitySettings.SetQualityLevel(qualityIndex); + } + + public void SetFullscreen(bool isFullscreen) + { + Screen.fullScreen = isFullscreen; + } +} diff --git a/botEasy.cs b/botEasy.cs new file mode 100644 index 0000000..d30e2b1 --- /dev/null +++ b/botEasy.cs @@ -0,0 +1,56 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class BotEasy : MonoBehaviour +{ + float speed = 0.5f; + public Transform Bola; + public Transform hitTarget; + public AudioClip hitSoundClip; + + float force = 6; + float jarakZDariBot = 7.5f; + float zMin = 7.2f; + float zMax = 9f; + + Vector3 targetPosition; + + void Start() + { + targetPosition = transform.position; + } + + void Update() + { + Move(); + + hitTarget.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - jarakZDariBot); + } + + void Move() + { + targetPosition.x = Bola.position.x; + float targetZ = Mathf.Clamp(Bola.position.z, zMin, zMax); + targetPosition.z = targetZ; + transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime); + } + + private void OnTriggerEnter(Collider other) + { + if (other.CompareTag("Bola")) + { + Vector3 dir = hitTarget.position - other.transform.position; + Rigidbody bolaRigidbody = other.GetComponent(); + + bolaRigidbody.useGravity = true; + + bolaRigidbody.velocity = dir.normalized * force + new Vector3(0, 6, 0); + + if (hitSoundClip != null) + { + AudioSource.PlayClipAtPoint(hitSoundClip, transform.position); + } + } + } +} diff --git a/botHard.cs b/botHard.cs new file mode 100644 index 0000000..34920ed --- /dev/null +++ b/botHard.cs @@ -0,0 +1,56 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class BotHard : MonoBehaviour +{ + float speed = 0.9f; + public Transform Bola; + public Transform hitTarget; + public AudioClip hitSoundClip; + + float force = 6; + float jarakZDariBot = 7.5f; + float zMin = 7.2f; + float zMax = 9f; + + Vector3 targetPosition; + + void Start() + { + targetPosition = transform.position; + } + + void Update() + { + Move(); + + hitTarget.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - jarakZDariBot); + } + + void Move() + { + targetPosition.x = Bola.position.x; + float targetZ = Mathf.Clamp(Bola.position.z, zMin, zMax); + targetPosition.z = targetZ; + transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime); + } + + private void OnTriggerEnter(Collider other) + { + if (other.CompareTag("Bola")) + { + Vector3 dir = hitTarget.position - other.transform.position; + Rigidbody bolaRigidbody = other.GetComponent(); + + bolaRigidbody.useGravity = true; + + bolaRigidbody.velocity = dir.normalized * force + new Vector3(0, 6, 0); + + if (hitSoundClip != null) + { + AudioSource.PlayClipAtPoint(hitSoundClip, transform.position); + } + } + } +} diff --git a/botMedium.cs b/botMedium.cs new file mode 100644 index 0000000..8f3e652 --- /dev/null +++ b/botMedium.cs @@ -0,0 +1,56 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class botMedium : MonoBehaviour +{ + float speed = 0.7f; + public Transform Bola; + public Transform hitTarget; + public AudioClip hitSoundClip; + + float force = 6; + float jarakZDariBot = 7.5f; + float zMin = 7.2f; + float zMax = 9f; + + Vector3 targetPosition; + + void Start() + { + targetPosition = transform.position; + } + + void Update() + { + Move(); + + hitTarget.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - jarakZDariBot); + } + + void Move() + { + targetPosition.x = Bola.position.x; + float targetZ = Mathf.Clamp(Bola.position.z, zMin, zMax); + targetPosition.z = targetZ; + transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime); + } + + private void OnTriggerEnter(Collider other) + { + if (other.CompareTag("Bola")) + { + Vector3 dir = hitTarget.position - other.transform.position; + Rigidbody bolaRigidbody = other.GetComponent(); + + bolaRigidbody.useGravity = true; + + bolaRigidbody.velocity = dir.normalized * force + new Vector3(0, 6, 0); + + if (hitSoundClip != null) + { + AudioSource.PlayClipAtPoint(hitSoundClip, transform.position); + } + } + } +} \ No newline at end of file