Skip to content

Instantly share code, notes, and snippets.

@satomi-i-k
Last active October 3, 2023 09:01
Show Gist options
  • Save satomi-i-k/34ecdaeb939ba3f3f8cae182d8b7303d to your computer and use it in GitHub Desktop.
Save satomi-i-k/34ecdaeb939ba3f3f8cae182d8b7303d to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
public class RubberBall : MonoBehaviour
{
[SerializeField]
GameObject Rubberball;
Rigidbody rb;
AudioSource audioSource;
public float serve_speed;
public float serve_forward;
public float serve_up;
public float timelag;
[HideInInspector]
public bool inactive;
[HideInInspector]
public bool after_hit = true;
public bool front_col;
public int floor_col;
bool playerHit;
public AudioClip criticalhit_sound;
public AudioClip hit_sound;
public AudioClip floor_sound;
public AudioClip wall_sound;
public AudioClip ceiling_sound;
public AudioClip shoot_sound;
public AudioClip lose;
public AudioClip notification;
public Raq_GameManager raq_gamemanager;
Raq_GameManager gamemanagerScript;
void Start()
{
rb = this.GetComponent<Rigidbody>();
raq_gamemanager = GameObject.FindObjectOfType<Raq_GameManager>();
gamemanagerScript = raq_gamemanager.GetComponent<Raq_GameManager>();
audioSource = GetComponent<AudioSource>();
}
void Update()
{
if (!gamemanagerScript.befServe)
{
Out();
}
}
private void OnEnable()
{
EventManager.OnRaqplayerServe += HandlePlayerServe;
EventManager.OnRaqplayerHit += HandlePlayerHit;
}
private void OnDisable()
{
EventManager.OnRaqplayerServe -= HandlePlayerServe;
EventManager.OnRaqplayerHit -= HandlePlayerHit;
}
private void HandlePlayerServe()
{
// プレイヤーの Serve時の処理
audioSource.PlayOneShot(shoot_sound);
rb.AddForce((transform.forward * serve_forward + transform.right + transform.up * serve_up) * serve_speed, ForceMode.VelocityChange);
gamemanagerScript.befServe = false;
front_col = false;
floor_col = 0;
after_hit = true;
Invoke(nameof(Cancel_Afthit), timelag);
}
//on collision, check what we hit and show an effect on the ground
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("FrontWall"))
{
front_col = true;
floor_col = 0;
if (rb.velocity.z >20 || rb.velocity.z < -20)
{
audioSource.PlayOneShot(criticalhit_sound);
}
else if (rb.velocity.z >= -20 && rb.velocity.z <= 20)
{
audioSource.PlayOneShot(hit_sound);
}
}
else if (other.gameObject.CompareTag("Floor"))
{
audioSource.PlayOneShot(floor_sound);
floor_col += 1;
}
else if (other.gameObject.CompareTag("Ceiling"))
{
audioSource.PlayOneShot(ceiling_sound);
}
else if (other.gameObject.CompareTag("Wall"))
{
audioSource.PlayOneShot(wall_sound);
}
if (inactive)
{
return;
}
}
private void HandlePlayerHit(Vector3 direction)
{
audioSource.PlayOneShot(shoot_sound);
rb.velocity = direction;
front_col = false;
floor_col = 0;
after_hit = true;
Invoke(nameof(Cancel_Afthit), timelag);
}
public void Cancel_Afthit()
{
after_hit = false;
}
public void Out()
{
// ショットする前にボールが床に2回以上に着いてしまった場合
if (front_col && floor_col >= 2 && !gamemanagerScript.befServe)
{
floor_col = 0;
raq_gamemanager.CourtTriggered(front_col);
}
// ショットしたボールが前の壁に届く前に床に着いてしまった場合(自分)
else if (GetLastHit() && !front_col && floor_col >= 1 && !gamemanagerScript.befServe)
{
floor_col = 0;
raq_gamemanager.CourtTriggered(front_col);
}
// ショットしたボールが前の壁に届く前に床に着いてしまった場合(相手プレイヤー)
else if (!GetLastHit() && !front_col && floor_col >= 2 && !gamemanagerScript.befServe)
{
floor_col = 0;
raq_gamemanager.CourtTriggered(front_col);
}
}
//set the last hit to player/opponent (to remember who hit the ball when the ball is out)
public void SetLastHit(bool player)
{
this.playerHit = player;
}
//retrieve the last hit info
public bool GetLastHit()
{
return this.playerHit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment