I am updating the rotation and position inside the database and sending it back to both clients . The problem is that the rotation is not working and only the position is updating. So I can see my car moving but always in the same direction as the car does not rotate on the other client.
IEnumerator moveBox(GameObject box, float x, float y, float rotatex, float rotatey, float rotatez)
{
//if ( isMoving ) yield break; // exit function
//isMoving = true;
if (box.GetComponent<Othersquarecontroller>().isMoving) yield break;
box.GetComponent<Othersquarecontroller>().isMoving = true;
Vector3 from = box.transform.position;
Vector3 to = new Vector3(x, y);
Quaternion rotation = box.transform.rotation;
Quaternion torotate = Quaternion.Euler(rotatex, rotatey, rotatez);
for (float t = 0f; t < 1f; t += Time.deltaTime * 5f)
{
box.transform.position = Vector3.Lerp(from, to, t);
box.transform.rotation = Quaternion.Lerp(rotation, torotate, t);
yield return null;
}
box.transform.position = to;
box.transform.rotation = torotate;
box.GetComponent<Othersquarecontroller>().isMoving = false;
}
IEnumerator getData()
{
while (true)
{
//checking the url that is being sent
Debug.Log(url + "?operation=poll");
//generate the url and send it to the website
UnityWebRequest www = UnityWebRequest.Get(url + "?operation=poll");
yield return www.SendWebRequest();
if (www.isHttpError || www.isNetworkError)
{
Debug.Log(www.error);
}
else
{
//this reads the text from the website
//Debug.Log(www.downloadHandler.text);
//split the data we get from the website.
string[] players = www.downloadHandler.text.Split('|');
//remove the hanging split
Array.Resize(ref players, players.Length - 1);
for (int index = 0; index < players.Length; index++)
{
Debug.Log(index + " " + players[index]);
//split again
string[] eachplayer = players[index].Split(',');
//remove the hanging comma
Array.Resize(ref eachplayer, eachplayer.Length);
foreach (string field in eachplayer)
{
//Debug.Log(field);
}
//id,name,xpos,ypos,
int id = int.Parse(eachplayer[0]);
string name = eachplayer[1];
float xpos = float.Parse(eachplayer[2]);
float ypos = float.Parse(eachplayer[3]);
color = eachplayer[4];
float rotationx = float.Parse(eachplayer[5]);
float rotationy = float.Parse(eachplayer[6]);
float rotationz = float.Parse(eachplayer[7]);
//the \ is an escape string, this line removes the single quote from the nicknames in the database if they have single quotes
name = name.Trim('\'');
if (GameObject.Find(eachplayer[0]) == null)
{
if (eachplayer[4] == "Red")
{
string urllcar2 = "http://127.0.0.1/game/AssetBundles/car2";
WWW www2 = new WWW(urllcar2);
while (!www2.isDone)
yield return null;
car = www2.assetBundle;
mycar = car.LoadAsset("car2") as GameObject;
otherplayer = Instantiate(mycar, new Vector3(xpos, ypos), Quaternion.identity);
otherplayer.GetComponentInChildren<TextMeshPro>().text = name;
otherplayer.name = eachplayer[0];
otherplayer.AddComponent<Othersquarecontroller>();
if (car != null)
{
car.Unload(false); //scene is unload from here
}
}
if (eachplayer[4] == "Green")
{
string urll = "http://127.0.0.1/game/AssetBundles/car";
WWW www1 = new WWW(urll);
while (!www1.isDone)
yield return null;
car = www1.assetBundle;
mycar = car.LoadAsset("car") as GameObject;
otherplayer = Instantiate(mycar, new Vector3(xpos, ypos), Quaternion.identity);
otherplayer.GetComponentInChildren<TextMeshPro>().text = name;
otherplayer.name = eachplayer[0];
otherplayer.AddComponent<Othersquarecontroller>();
if (car != null)
{
car.Unload(false); //scene is unload from here
}
}
if (eachplayer[4] == "Yellow")
{
string urllcar1 = "http://127.0.0.1/game/AssetBundles/car1";
WWW www3 = new WWW(urllcar1);
while (!www3.isDone)
yield return null;
car = www3.assetBundle;
mycar = car.LoadAsset("car1") as GameObject;
otherplayer = Instantiate(mycar, new Vector3(xpos, ypos), Quaternion.identity);
otherplayer.GetComponentInChildren<TextMeshPro>().text = name;
otherplayer.name = eachplayer[0];
otherplayer.AddComponent<Othersquarecontroller>();
if (car != null)
{
car.Unload(false); //scene is unload from here
}
}
}
else
{
if (eachplayer[0] != loadedplayer.name)
{
//update other player position
otherplayer = GameObject.Find(eachplayer[0]);
// otherplayer.transform.position = new Vector3(xpos, ypos);
StartCoroutine(moveBox(otherplayer,xpos , ypos, rotationx, rotationy, rotationz));
}
}
}
}
}
}