Hello,
Not sure if I’m asking in the right area, but here goes! I’m attempting to set up some multiplayer action using Photon (first time so please bear with me). I have a robot character that fires a large projectile from its arm, or at least it’s supposed to. Two issues… Issue number one is that when the projectile is instantiated, on the other players screen, it sometimes is instantiated behind the position it’s supposed to. This results in the robot shooting itself in the elbow. Issue number two is that since there’s a tiny bit of lag between what one player is doing and what the other sees, when the projectile collides with an object, it appears to stop in mid air on the other players screen. I believe this second issue has to do with my AmmoNetworkMover script listed below: (I’m guessing this because the bullet no longer has anything to Lerp to when it’s destroyed)
public class AmmoNetworkMover : Photon.MonoBehaviour
{
private Vector3 position;
private Quaternion rotation;
private float smoothing = 10.0f;
void Start()
{
if (!photonView.isMine)
{
StartCoroutine(“UpdateData”);
}
}
IEnumerator UpdateData()
{
while (true)
{
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * smoothing);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * smoothing);
yield return null;
}
}
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
position = (Vector3)stream.ReceiveNext();
rotation = (Quaternion)stream.ReceiveNext();
}
}
}
Here is the projectile script:
public class PPC_Ammo_Script : Photon.MonoBehaviour
{
public Transform explosionPrefab;
public Vector3 offset = new Vector3(0, -1, 0);
public float moveSpeed = 50;
public float lifeTime = 3.0f;
public AudioSource audioSource;
public AudioClip weaponFireSound;
private bool leftArmFiredLast = true;
private float smoothing = 10f;
private Vector3 position;
private Quaternion rotation;
void Start()
{
Destroy(gameObject, lifeTime);
if (photonView.isMine)
{
audioSource.clip = weaponFireSound;
audioSource.Play();
}
else
{
this.enabled = false;
}
}
void Update()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
void OnCollisionEnter()
{
GameObject.Instantiate(explosionPrefab, transform.position + offset, Quaternion.identity);
Destroy(gameObject);
}
}
And lastly, here’s the NetworkFireWeapon script that instantiates the projectiles:
public class NetworkFireWeapon : Photon.MonoBehaviour
{
public Transform leftArmFireTrans;
public Transform rightArmFireTrans;
private bool leftArmFiredLast = true;
void Start()
{
if (photonView.isMine)
{
}
else
{
this.enabled = false;
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (leftArmFiredLast)
{
PhotonNetwork.Instantiate(“PPC”, rightArmFireTrans.position, rightArmFireTrans.rotation, 0);
leftArmFiredLast = false;
}
else
{
PhotonNetwork.Instantiate(“PPC”, leftArmFireTrans.position, leftArmFireTrans.rotation, 0);
leftArmFiredLast = true;
}
}
}
Thanks for taking a look!