Script untuk objek Bola, Bot dan UI Settings
This commit is contained in:
AryPratama 2024-06-10 14:30:26 +07:00
parent d4fe831535
commit bb9de20d39
5 changed files with 345 additions and 0 deletions

105
Bola.cs Normal file
View File

@ -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<Rigidbody>().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<Rigidbody>().velocity = Vector3.zero;
GetComponent<Rigidbody>().useGravity = false;
lastHitPlayerMeja = false;
lastHitBotMeja = false;
}
void ResetBolaPosition(float zPosition)
{
Vector3 newPosition = new Vector3(0.86f, 5.65f, zPosition);
transform.position = newPosition;
GetComponent<Rigidbody>().velocity = Vector3.zero;
GetComponent<Rigidbody>().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);
}
}
}

72
SettingsMenu.cs Normal file
View File

@ -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<string> resolutionOptions = new List<string>();
List<string> qualityOptions = new List<string>();
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;
}
}

56
botEasy.cs Normal file
View File

@ -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<Rigidbody>();
bolaRigidbody.useGravity = true;
bolaRigidbody.velocity = dir.normalized * force + new Vector3(0, 6, 0);
if (hitSoundClip != null)
{
AudioSource.PlayClipAtPoint(hitSoundClip, transform.position);
}
}
}
}

56
botHard.cs Normal file
View File

@ -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<Rigidbody>();
bolaRigidbody.useGravity = true;
bolaRigidbody.velocity = dir.normalized * force + new Vector3(0, 6, 0);
if (hitSoundClip != null)
{
AudioSource.PlayClipAtPoint(hitSoundClip, transform.position);
}
}
}
}

56
botMedium.cs Normal file
View File

@ -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<Rigidbody>();
bolaRigidbody.useGravity = true;
bolaRigidbody.velocity = dir.normalized * force + new Vector3(0, 6, 0);
if (hitSoundClip != null)
{
AudioSource.PlayClipAtPoint(hitSoundClip, transform.position);
}
}
}
}