镜像(粒子系统)统一问题,请帮助:)
problem with mirror(particle system) unity, pls help:)
我有一个关于镜子的问题,我正在制作一个在线战略游戏,你在游戏中有建筑,你可以用这些建筑来制造士兵,现在这些建筑有一个粒子模拟来工作,同时它们会变得更坚固, 但只有服务器可以看到自己建筑物和对手的粒子模拟,有什么想法吗?
这是构建烟雾的代码:
#region data
[SerializeField] private ParticleSystem[] Explotion = new ParticleSystem[2];
[SerializeField] private Unit UnitPrefab;
[SerializeField] private Transform position;
[SerializeField] private Health MyHp;
[SerializeField] private Image ProgressBar = null;
[SerializeField] private TMP_Text QuaeText = null;
[SerializeField] private float QDurition = 5f;
[SerializeField] private float SpaceSpawn = 7f;
[SerializeField] private int QMax = 5;
[SerializeField] private float progressImageVelocity = 2;
bool played = false;
[SyncVar(hook = nameof(UpdateQText))] private int QuadeUnits;
[SyncVar] private float Timer = 0f;
private ParticleSystem Smoke = null;
private RTSPlayer player;
private float QProgress;
#endregion
//[ServerCallback]
private void UpdateQText(int OldQ,int NewQ)
{
QuaeText.text = NewQ.ToString();
}
private void Update()
{
if(Smoke == null)
{
Smoke = GetComponentInChildren<ParticleSystem>();
}
if(player == null)
{
player = NetworkClient.connection.identity.GetComponent<RTSPlayer>();
}
if (isServer)
{
ProduceUnits();
}
if (isClient)
{
UpdateTimer();
}
if(QuadeUnits <= 0)
{
QuadeUnits = 0;
Timer = 0;
QProgress = 0;
ProgressBar.fillAmount = 0;
}
}
[Server]
private void ProduceUnits()
{
if(QuadeUnits == 0)
{
try
{
Smoke.Stop();
}
catch
{
}
played = false;
return;
}
try
{
if (!played)
{
print("playing");
Smoke.Play();
played = true;
}
}
catch
{
}
Timer += Time.deltaTime;
if(Timer < QDurition) { return; }
GameObject instance1 = Instantiate(UnitPrefab.gameObject, position.position, position.rotation);
NetworkServer.Spawn(instance1, connectionToClient);
Vector3 SpawnPlace = gameObject.transform.position * SpaceSpawn;
SpawnPlace.y = 0;
Movment ClearSpot = instance1.gameObject.GetComponent<Movment>();
ClearSpot.ServerMoveUnit(SpawnPlace);
instance1.gameObject.GetComponent<HPDispaly>().SetEnacled(false);
QuadeUnits--;
Timer = 0f;
}
这不是很有用,但我发现了其他代码可以帮助我解决这个问题,如果有人有同样的问题,这里是解决方案:
Solution
您需要创建一个 syncVar bool 并使用它更新动画师,而不仅仅是常规 false/true 调用,这是我的新代码:
#region data
[SyncVar(hook = nameof(HandleBuilding))] private bool IsBuildng = false;
[SyncVar(hook = nameof(HandleExplotion))] private bool IsPlaying = false;
[SerializeField] private ParticleSystem[] Explotion = new ParticleSystem[2];
[SerializeField] private Unit UnitPrefab;
[SerializeField] private Transform position;
[SerializeField] private Health MyHp;
[SerializeField] private Image ProgressBar = null;
[SerializeField] private TMP_Text QuaeText = null;
[SerializeField] private float QDurition = 5f;
[SerializeField] private float SpaceSpawn = 7f;
[SerializeField] private int QMax = 5;
[SerializeField] private float progressImageVelocity = 2;
bool played = false;
[SyncVar(hook = nameof(UpdateQText))] private int QuadeUnits;
[SyncVar] private float Timer = 0f;
[SerializeField] private ParticleSystem Smoke = null;
private RTSPlayer player;
private float QProgress;
#endregion
//[ServerCallback]
private void UpdateQText(int OldQ,int NewQ)
{
QuaeText.text = NewQ.ToString();
}
private void HandleBuilding(bool OldBool,bool NewBool)
{
Smoke.gameObject.SetActive(IsBuildng);
}
private void Update()
{
if(Smoke == null)
{
Smoke = GetComponentInChildren<ParticleSystem>();
}
if(player == null)
{
player = NetworkClient.connection.identity.GetComponent<RTSPlayer>();
}
if (isServer)
{
ProduceUnits();
}
if (isClient)
{
UpdateTimer();
}
if(QuadeUnits <= 0)
{
QuadeUnits = 0;
Timer = 0;
QProgress = 0;
ProgressBar.fillAmount = 0;
}
}
[Server]
private void ProduceUnits()
{
if(QuadeUnits == 0)
{
IsBuildng = false;
played = false;
return;
}
else
{
IsBuildng = true;
}
IsBuildng = true;
Timer += Time.deltaTime;
if(Timer < QDurition) { return; }
GameObject instance1 = Instantiate(UnitPrefab.gameObject, position.position, position.rotation);
NetworkServer.Spawn(instance1, connectionToClient);
Vector3 SpawnPlace = gameObject.transform.position * SpaceSpawn;
SpawnPlace.y = 0;
Movment ClearSpot = instance1.gameObject.GetComponent<Movment>();
ClearSpot.ServerMoveUnit(SpawnPlace);
instance1.gameObject.GetComponent<HPDispaly>().SetEnacled(false);
QuadeUnits--;
Timer = 0f;
}
我有一个关于镜子的问题,我正在制作一个在线战略游戏,你在游戏中有建筑,你可以用这些建筑来制造士兵,现在这些建筑有一个粒子模拟来工作,同时它们会变得更坚固, 但只有服务器可以看到自己建筑物和对手的粒子模拟,有什么想法吗?
这是构建烟雾的代码:
#region data
[SerializeField] private ParticleSystem[] Explotion = new ParticleSystem[2];
[SerializeField] private Unit UnitPrefab;
[SerializeField] private Transform position;
[SerializeField] private Health MyHp;
[SerializeField] private Image ProgressBar = null;
[SerializeField] private TMP_Text QuaeText = null;
[SerializeField] private float QDurition = 5f;
[SerializeField] private float SpaceSpawn = 7f;
[SerializeField] private int QMax = 5;
[SerializeField] private float progressImageVelocity = 2;
bool played = false;
[SyncVar(hook = nameof(UpdateQText))] private int QuadeUnits;
[SyncVar] private float Timer = 0f;
private ParticleSystem Smoke = null;
private RTSPlayer player;
private float QProgress;
#endregion
//[ServerCallback]
private void UpdateQText(int OldQ,int NewQ)
{
QuaeText.text = NewQ.ToString();
}
private void Update()
{
if(Smoke == null)
{
Smoke = GetComponentInChildren<ParticleSystem>();
}
if(player == null)
{
player = NetworkClient.connection.identity.GetComponent<RTSPlayer>();
}
if (isServer)
{
ProduceUnits();
}
if (isClient)
{
UpdateTimer();
}
if(QuadeUnits <= 0)
{
QuadeUnits = 0;
Timer = 0;
QProgress = 0;
ProgressBar.fillAmount = 0;
}
}
[Server]
private void ProduceUnits()
{
if(QuadeUnits == 0)
{
try
{
Smoke.Stop();
}
catch
{
}
played = false;
return;
}
try
{
if (!played)
{
print("playing");
Smoke.Play();
played = true;
}
}
catch
{
}
Timer += Time.deltaTime;
if(Timer < QDurition) { return; }
GameObject instance1 = Instantiate(UnitPrefab.gameObject, position.position, position.rotation);
NetworkServer.Spawn(instance1, connectionToClient);
Vector3 SpawnPlace = gameObject.transform.position * SpaceSpawn;
SpawnPlace.y = 0;
Movment ClearSpot = instance1.gameObject.GetComponent<Movment>();
ClearSpot.ServerMoveUnit(SpawnPlace);
instance1.gameObject.GetComponent<HPDispaly>().SetEnacled(false);
QuadeUnits--;
Timer = 0f;
}
这不是很有用,但我发现了其他代码可以帮助我解决这个问题,如果有人有同样的问题,这里是解决方案: Solution
您需要创建一个 syncVar bool 并使用它更新动画师,而不仅仅是常规 false/true 调用,这是我的新代码:
#region data
[SyncVar(hook = nameof(HandleBuilding))] private bool IsBuildng = false;
[SyncVar(hook = nameof(HandleExplotion))] private bool IsPlaying = false;
[SerializeField] private ParticleSystem[] Explotion = new ParticleSystem[2];
[SerializeField] private Unit UnitPrefab;
[SerializeField] private Transform position;
[SerializeField] private Health MyHp;
[SerializeField] private Image ProgressBar = null;
[SerializeField] private TMP_Text QuaeText = null;
[SerializeField] private float QDurition = 5f;
[SerializeField] private float SpaceSpawn = 7f;
[SerializeField] private int QMax = 5;
[SerializeField] private float progressImageVelocity = 2;
bool played = false;
[SyncVar(hook = nameof(UpdateQText))] private int QuadeUnits;
[SyncVar] private float Timer = 0f;
[SerializeField] private ParticleSystem Smoke = null;
private RTSPlayer player;
private float QProgress;
#endregion
//[ServerCallback]
private void UpdateQText(int OldQ,int NewQ)
{
QuaeText.text = NewQ.ToString();
}
private void HandleBuilding(bool OldBool,bool NewBool)
{
Smoke.gameObject.SetActive(IsBuildng);
}
private void Update()
{
if(Smoke == null)
{
Smoke = GetComponentInChildren<ParticleSystem>();
}
if(player == null)
{
player = NetworkClient.connection.identity.GetComponent<RTSPlayer>();
}
if (isServer)
{
ProduceUnits();
}
if (isClient)
{
UpdateTimer();
}
if(QuadeUnits <= 0)
{
QuadeUnits = 0;
Timer = 0;
QProgress = 0;
ProgressBar.fillAmount = 0;
}
}
[Server]
private void ProduceUnits()
{
if(QuadeUnits == 0)
{
IsBuildng = false;
played = false;
return;
}
else
{
IsBuildng = true;
}
IsBuildng = true;
Timer += Time.deltaTime;
if(Timer < QDurition) { return; }
GameObject instance1 = Instantiate(UnitPrefab.gameObject, position.position, position.rotation);
NetworkServer.Spawn(instance1, connectionToClient);
Vector3 SpawnPlace = gameObject.transform.position * SpaceSpawn;
SpawnPlace.y = 0;
Movment ClearSpot = instance1.gameObject.GetComponent<Movment>();
ClearSpot.ServerMoveUnit(SpawnPlace);
instance1.gameObject.GetComponent<HPDispaly>().SetEnacled(false);
QuadeUnits--;
Timer = 0f;
}