Photon projectile issues

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!

To solve the bullet not moving, you can extrapolate a velocity from position updates, and keep on moving the projectile by that velocity after it is no longer receiving updates/its target is destroyed.

I think what’s happening with your robot shooting itself in the elbow is, you need to spawn the rocket on the server. If you spawn it on the client, you’re using an approximation of the actual position.

Ah, very interesting. Thanks your response. I spent a lot of time on it today and couldn’t figure it out so I decided to try and come up with a different solution. What I came up with is kinda sneaky and probably in no way a good idea but it kinda works.

Basically what I do is, is fire two projectiles that represent one bullet. One is the ‘real’ bullet, that only the player who fired sees, and the other is a fake bullet that the other players see. For the real bullet, its network counterpart (!isMine), that all the other players would see, is instantly destroyed.

The fake bullet is still shot from the same position, but it’s instantiated and animated on the receiving players machine. Like the real bullet, this bullet only exists on the local players machine and its network counterpart is also destroyed in the Start function.

Here’s the catch, the fake bullet doesn’t do anything except for get destroyed when it collides with something. The real bullet is the only one that ‘inflicts’ damage. But the game is very fast paced so so far it seems like an okay solution. (But I’m all ears as to why it might not be)

I’m sure I’m going to find all sorts of problems with this method but oh well, I guess that’s just the way she goes bubs.

Hi! I’m having similar issues…When I cast a ray(for shooting purposes), the x&z co-ordinates match perfectly but sadly the y doesn’t. I’m making a multiplayer FPS. So, it pretty much renders my game useless…Any idea how to fix this? I’m NEW to Photon.