
今回はフォールドした時のチップのアニメーションと、取得したチップの処理について考えました。プレイヤーがフォールドした場合、フォールドした逆のプレイヤーの方にチップが集められ、チップスタックが増えるという動きを実装します。
まずはフォールドした時に押すボタンを作成します。プレイヤー1,2別に位置が変わるようにしてあります。

それから、フォールドしたプレイヤーを特定して相手にその情報を送る必要があるので、RoomCustomPropertiesを使用してボタンを押したプレイヤー情報を取得しています。
public class FoldButton : MonoBehaviour {
public GameObject foldArea;
int buttonX;
float buttonPosition;
public Vector3 temp;
public int foldPlayer;
void OnJoinedRoom()
{
if (PhotonNetwork.player.ID == 1)
{
buttonPosition = 4.71f;
buttonX = 980;
}
else
{
buttonPosition = -4.91f;
buttonX = 10;
}
}
void OnGUI()
{
if (GUI.Button(new Rect(buttonX, 460, 35, 90), "fold")) //HITMEを押したら下記を実行
{
GameObject foldArea = (GameObject)PhotonNetwork.Instantiate("FoldArea", temp, Quaternion.identity, 0);
foldPlayer = PhotonNetwork.player.ID;
var properties = new ExitGames.Client.Photon.Hashtable();
properties.Add("foldPlayer", foldPlayer);
PhotonNetwork.room.SetCustomProperties(properties);
}
}
}
チップが集められる方向を決めるオブジェクトを作成します。Destination1,2というCubeを両側に配置し、Rigidbody2DとBoxCollider2Dを付けます。


プレイヤー1側がフォールドした場合逆側のキューブにチップが流れていくようにスクリプトを考えました。まずは、中央のチップが掛けられているフィールドを覆う為に十分な大きさのオブジェクトをPrefab化してから、Instantiateで中央に表示します。後でスプライトレンダラーをディアクティベートしてオブジェクトは見えなくします。

そして、掛けられているチップとの衝突判定をし、衝突しているチップには事前に作成したキューブに向かって、加速度を与えてあげます。加速度は下記のリンクを参考にしました。
Instantiateされるオブジェクトには以下の、ChipMover.csをアタッチしてあります。
public class ChipMover : MonoBehaviour {
public GameObject destination;
public GameObject destination2;
public float accelerationScale;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("100Chip") || other.gameObject.CompareTag("500Chip"))
{
if ((int)PhotonNetwork.room.customProperties["foldPlayer"] == 2)
{
var direction = destination.transform.position - other.transform.position;
direction.Normalize();
Rigidbody2D rigidbody2D = other.GetComponent<Rigidbody2D>();
rigidbody2D.AddForce(accelerationScale * direction, ForceMode2D.Force);
}
else
{
var direction = destination2.transform.position - other.transform.position;
direction.Normalize();
Rigidbody2D rigidbody2D = other.GetComponent<Rigidbody2D>();
rigidbody2D.AddForce(accelerationScale * direction, ForceMode2D.Force);
}
}
}
}
ここで引っかかったのは、collider2DをデフォルトでオフにしてIsMineでオンにしている事を忘れていたせいで、衝突判定が正常に行われなかった事でした。気付いてからは、取り急ぎ全部オンにして進めました。相手のチップが触れてしまうのですが、後で工夫を考える事にします。
destinationのcubeには以下のスクリプトChipGetter.cs をアタッチします。衝突判定を行い、ぶつかった平面チップをデストロイし、立面のチップを上から落としてスタックに加えます。
public class ChipGetter : MonoBehaviour
{
public ChipCalculator chipCalculator;
public Vector3 temp;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("100Chip"))
{
Destroy(other.gameObject); //ぶつかった相手をデストロイ(消える
if (PhotonNetwork.player.ID == 2)
{
GameObject pokerchip = (GameObject)PhotonNetwork.Instantiate("pokerchip4", temp, Quaternion.identity, 0);
}
}else if(other.gameObject.CompareTag("500Chip"))
{
Destroy(other.gameObject); //ぶつかった相手をデストロイ(消える
if (PhotonNetwork.player.ID == 2)
{
GameObject pokerchip = (GameObject)PhotonNetwork.Instantiate("pokerchip3", temp, Quaternion.identity, 0);
}
}
}
}
実行すると、下記のGIFようにチップが取得されることが確認できます。(クリックでリンク先に飛べます。)










