MIF_E31211305/Bola.cs

105 lines
2.8 KiB
C#

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);
}
}
}