Photon - Color Sync,PhotonUnityNetwork - Online cars game change car colors
Hello everyone :)
Scenario: Tuning: A garage with an BMW car. Each user change his car color in garage. Photon Multiplayer: All users join in a map, and each user can see the other user tuned car color.
Problem: Color sync ( the car instance is from the same prefab, for each user ), when I try to change material color for 1 user, the others get the same color.
My code: using UnityEngine; using System.Collections; using System.Collections.Generic;
[RequireComponent(typeof(PhotonView))]
public class RCC_PhotonNetwork : Photon.MonoBehaviour {
// Main Car Controller, Rigidbody, and Main RCC Camera of the scene.
private RCC_CarControllerV3 carController;
private Rigidbody rigid;
private RCC_Camera rccCamera;
// Car's position and rotation. We will send these to server.
private Vector3 correctPlayerPos;
private Quaternion correctPlayerRot;
// Used for projected (interpolated) position.
private Vector3 currentVelocity;
private float updateTime = 0;
// All inputs for RCC. We will send these values if we own this vehicle. If this vehicle is owned by other player, receives all these inputs from server.
private float gasInput = 0f;
private float brakeInput = 0f;
private float steerInput = 0f;
private float handbrakeInput = 0f;
private float boostInput = 0f;
private float clutchInput = 0f;
private float idleInput = 0f;
private int gear = 0;
private int direction = 1;
private bool changingGear = false;
private bool semiAutomaticGear = false;
private float fuelInput = 1f;
private bool engineRunning = false;
// Lights.
private bool lowBeamHeadLightsOn = false;
private bool highBeamHeadLightsOn = false;
// For Indicators.
private RCC_CarControllerV3.IndicatorsOn indicatorsOn;
// Car color
public Material m_MyCarMaterial;
public Color m_MyCarColor;
public Color m_OtherCarColor;
//private List<GameObject> color_materials = new List<GameObject>();
// private GameObject color_tag_game_object;
void Awake(){
// Getting Main Car Controller, Rigidbody, and Main RCC Camera of the scene.
rccCamera = GetComponent<RCC_Camera>();
carController = GetComponent<RCC_CarControllerV3>();
rigid = GetComponent<Rigidbody>();
// If we are the owner of this vehicle, enable Main RCC Camera, and enable Car Controller. Do opposite if another player owned this vehicle.
if (photonView.isMine){
if(rccCamera)
rccCamera.gameObject.SetActive(true);
carController.canControl = true;
}else{
if(rccCamera)
rccCamera.gameObject.SetActive(false);
carController.canControl = false;
}
// Setting name of the gameobject with Photon View ID.
gameObject.name = gameObject.name + photonView.viewID;
}
void Start(){
// PhotonNetwork.sendRate = 60;
// PhotonNetwork.sendRateOnSerialize = 60;
}
public void FixedUpdate(){
// If we are not owner of this vehicle, receive all inputs from server.
if (!photonView.isMine) {
carController.AIController = true;
Vector3 projectedPosition = this.correctPlayerPos + currentVelocity * (Time.time - updateTime);
if(Vector3.Distance(transform.position, correctPlayerPos) < 15f){
transform.position = Vector3.Lerp (transform.position, projectedPosition, Time.deltaTime * 5f);
transform.rotation = Quaternion.Lerp (transform.rotation, this.correctPlayerRot, Time.deltaTime * 5f);
}else{
transform.position = correctPlayerPos;
transform.rotation = correctPlayerRot;
}
carController.gasInput = gasInput;
carController.brakeInput = brakeInput;
carController.steerInput = steerInput;
carController.handbrakeInput = handbrakeInput;
carController.boostInput = boostInput;
carController.clutchInput = clutchInput;
carController.idleInput = idleInput;
carController.currentGear = gear;
carController.direction = direction;
carController.changingGear = changingGear;
carController.semiAutomaticGear = semiAutomaticGear;
carController.fuelInput = fuelInput;
carController.engineRunning = engineRunning;
carController.lowBeamHeadLightsOn = lowBeamHeadLightsOn;
carController.highBeamHeadLightsOn = highBeamHeadLightsOn;
carController.indicatorsOn = indicatorsOn;
m_MyCarMaterial.SetColor("_Color", m_OtherCarColor);
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
// Sending all inputs, position, rotation, and velocity to server.
if (stream.isWriting){
//We own this player: send the others our data
stream.SendNext(carController.gasInput);
stream.SendNext(carController.brakeInput);
stream.SendNext(carController.steerInput);
stream.SendNext(carController.handbrakeInput);
stream.SendNext(carController.boostInput);
stream.SendNext(carController.clutchInput);
stream.SendNext(carController.idleInput);
stream.SendNext(carController.currentGear);
stream.SendNext(carController.direction);
stream.SendNext(carController.changingGear);
stream.SendNext(carController.semiAutomaticGear);
stream.SendNext(carController.fuelInput);
stream.SendNext(carController.engineRunning);
stream.SendNext(carController.lowBeamHeadLightsOn);
stream.SendNext(carController.highBeamHeadLightsOn);
stream.SendNext(carController.indicatorsOn);
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
stream.SendNext(rigid.velocity);
//send color
Vector3 tempcolor = new Vector3(m_MyCarMaterial.color.r, m_MyCarMaterial.color.g, m_MyCarMaterial.color.b);
stream.SendNext(tempcolor);
}else{
// Network player, receiving all inputs, position, rotation, and velocity from server.
gasInput = (float)stream.ReceiveNext();
brakeInput = (float)stream.ReceiveNext();
steerInput = (float)stream.ReceiveNext();
handbrakeInput = (float)stream.ReceiveNext();
boostInput = (float)stream.ReceiveNext();
clutchInput = (float)stream.ReceiveNext();
idleInput = (float)stream.ReceiveNext();
gear = (int)stream.ReceiveNext();
direction = (int)stream.ReceiveNext();
changingGear = (bool)stream.ReceiveNext();
semiAutomaticGear = (bool)stream.ReceiveNext();
fuelInput = (float)stream.ReceiveNext();
engineRunning = (bool)stream.ReceiveNext();
lowBeamHeadLightsOn = (bool)stream.ReceiveNext();
highBeamHeadLightsOn = (bool)stream.ReceiveNext();
indicatorsOn = (RCC_CarControllerV3.IndicatorsOn)stream.ReceiveNext();
correctPlayerPos = (Vector3)stream.ReceiveNext();
correctPlayerRot = (Quaternion)stream.ReceiveNext();
currentVelocity = (Vector3)stream.ReceiveNext();
Vector3 tempcolor = (Vector3)stream.ReceiveNext();
m_OtherCarColor = new Color(tempcolor.x, tempcolor.y, tempcolor.z, 1.0f);
updateTime = Time.time;
}
}
}
Answer by ChristianSimon · Jun 14, 2018 at 12:23 PM
Hi,
you have different possibilities to solve this problem. The first option is to instantiate a car prefab for a client just locally, so that he can apply his modifications just for himself. When the map is loaded he can network-instantiate the car prefab and apply the modifications to it (for example by using RPC calls or the OnPhotonSerializeView function). You would have to store these changes in order to apply them easily.
Another option is to network-instantiate the car prefab already in the garage, but don't render it on remote clients (and also disable its collider). This way each client will apply the changes as soon as they are made by the owner, but aren't visible, until the car renderer gets enabled again.
Your answer
![](https://koobas.hobune.stream/wayback/20220612164306im_/https://answers.unity.com/themes/thub/images/avi.jpg)