Both players reach same time how to get result draw in photon unity
I've been working on a photon multiplayer racing game. The main problem is how to synchronize both players reaching the end line at the same time. My main intention is to show a "Draw/Tie" message to both players.
void OnCollisionEnter(Collision others)
{
if (others.gameObject.tag == "End")
{
Debug.Log ("JJJJJJJJJJ");
if (m_PhotonView.isMine)
{
GameObject prefab = Instantiate (WinPrefab, new Vector3 (0, 0, 0), Quaternion.identity) as GameObject;
prefab.transform.SetParent (GameObject.FindGameObjectWithTag ("Canvas").transform, false);
Debug.Log ("Winner Position"+this.Position);
Debug.Log ("you are Winner");
//Invoke ("PositionChange", 3.0f);
//Destroy (this.gameObject);
//transform.position = Position;
//PhotonNetwork.DestroyAll ();
}
if (!m_PhotonView.isMine)
{
GameObject prefab = Instantiate (LoosePrefab, new Vector3 (0, 0, 0), Quaternion.identity) as GameObject;
prefab.transform.SetParent (GameObject.FindGameObjectWithTag ("Canvas").transform, false);
Debug.Log ("You are looser");
//Invoke ("PositionChange", 3.0f);
//Destroy (this.gameObject);
//PhotonNetwork.DestroyAll ();
}
if (m_PhotonView.isMine && !m_PhotonView.isMine)
{
GameObject prefab = Instantiate (DrawPrefab, new Vector3 (0, 0, 0), Quaternion.identity) as GameObject;
prefab.transform.SetParent (GameObject.FindGameObjectWithTag ("Canvas").transform, false);
Debug.Log ("Match Drawn");
}
//PhotonNetwork.DestroyAll ();
}
Answer by mmciver · Feb 18, 2017 at 04:57 AM
My first reaction would be to add an empty game object at the end collider, and check the distance from the object to each player, if they are the same (or close enough?) then declare it a draw.
However, if the collider they are hitting is flat (and it probably should be) and point at which each player could hit it is not controllable, then the above method could lead to a scenario where the 'losing' player is closer to the game object than the winning player.
My 2nd solution would be to ensure that the end collider is always squarely on one of the axis, so you can simply check the transform.position.x or .y or .z values (whichever is appropriate) to see if which is greater, or if they are equal.
If you can't ensure this situation, then you can modify it by getting the negative normal of the collider plane (so pointing from the collider in the direction of the player motion), then projecting the vector of each player onto this vector and seeing which is greater, or if they are equal.
Your answer
Follow this Question
Related Questions
PhotonOnSerializeView() Only working sometimes 0 Answers
Photon ccu question 0 Answers
Match making in Photon 0 Answers
Unity Photon Networking Buttons ~ HELP, 0 Answers