【发布时间】:2021-06-24 08:47:16
【问题描述】:
我正在尝试播放和停止作为玩家预制子的粒子系统。到目前为止,我已经让主机客户端的推力粒子效果在所有客户端上更新,但我无法找出非主机客户端。我希望玩家每次按下推进器按钮时,游戏都会在所有客户端上显示推进器效果,并且当它们停止时,粒子系统会停止并且其他客户端也会看到它。
目前我的代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class PlayerMovement : NetworkBehaviour
{
public float thrustPower = 300f;
Rigidbody2D m_playerRb;
bool m_thrustOn = false;
public ParticleSystem m_playerPs;
[SyncVar(hook = nameof(SetThrusters))]
bool m_thrusterState = false;
private void Start()
{
m_playerRb = GetComponent<Rigidbody2D>();
m_playerPs.Stop();
}
private void Update()
{
if (isLocalPlayer)
{
MovePlayer();
m_thrusterState = m_thrustOn;
}
}
private void FixedUpdate()
{
if (m_thrustOn)
{
m_playerRb.AddForce(gameObject.transform.up * thrustPower * Time.fixedDeltaTime, ForceMode2D.Force);
}
}
private void SetThrusters(bool oldVal, bool newVal)
{
if (newVal) m_playerPs.Play();
else if (newVal == false) m_playerPs.Stop();
}
private void MovePlayer()
{
m_thrustOn = Input.GetKey(KeyCode.W) ? true : false;
float horizontalInput = Input.GetAxisRaw("Horizontal");
transform.Rotate(new Vector3(0, 0, horizontalInput * -180 * Time.deltaTime));
}
}
在粒子系统方面,我真的无法在网上找到任何关于 Mirror 的信息。如果您需要更多信息,请告诉我。
谢谢!
【问题讨论】: