[SOLVED] Projectiles Disappearing

Options
Hey all! I've been a really happy user of PUN for the past couple of weeks now, but I've seem to run into a roadblock. You can test the current state of my project, which is a third person shooter, here: https://dl.dropboxusercontent.com/u/18297273/WebTest/WebTest.html

Right now, the main problem lies in the Grenade Launcher's projectiles. Although they can be shot out and appear alright, they don't explode reliably. I call the explosion and it does cause a explosion, but from the Debug log, sometimes the Coroutine to countdown the grenade's fuse sometimes doesn't execute the actual explosion function.

To the user, this means that sometimes you can launch grenades which explode normally, but also have times where none or only some of your grenades explode.

Here's the code for the projectile:
[code2=csharp]using UnityEngine;
using System.Collections;

public class GrenadeProjectile : Photon.MonoBehaviour {

public float ExForce = 20;
public float ExRadius = 3;
public float UpForce = 10;
public float Fuse = 3;
public float IgnoreTime = 0.25f;
public bool detonate = false;
private bool contact = false;
// Use this for initialization
void Start () {
StartCoroutine(ExplodeInSeconds(Fuse));
IgnoreTime = Time.time + IgnoreTime;
}

// Update is called once per frame
void Update () {
if (!contact){
transform.rotation = Quaternion.LookRotation(GetComponent<Rigidbody>().velocity) * Quaternion.Euler(-90, 0, 0);
}
}

void OnCollisionEnter(Collision collision) {
if (collision.collider.tag != "Player")
contact = true;
else if (Time.time > IgnoreTime)
Explode();

}

IEnumerator ExplodeInSeconds(float Fuse) {
yield return new WaitForSeconds(Fuse);
Explode();
}

public void Explode() {
if (GameObject.Instantiate(Resources.Load("ExplosionEffect", typeof(GameObject)) , transform.position, Quaternion.identity)) {
detonate = true;
GameObject.Destroy(this.gameObject, 0.01f);
}
}
}[/code2]

And here's the code for the Grenade Launcher:

[code2=csharp]using UnityEngine;
using System.Collections;

public class WeaponGrenadeLauncher : Photon.MonoBehaviour {

public NetworkManager NetScript;
public float DischargeForce = 30;
public float FireDelay = 0.5f;
public AudioClip FireSound;
private float fireCool = 0;
private GameObject LauncherObject;
private Quaternion rotation;
private Camera Cam;
private RaycastHit aimtarget;
private GameObject Grenade;

private Vector3 LaunchPos;
private Quaternion LaunchRot;

// Use this for initialization
void Start () {
LauncherObject = GameObject.Find("GLauncher");
NetScript = GameObject.FindGameObjectWithTag("NetHandler").GetComponent<NetworkManager>();
Cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();

}

// Update is called once per frame
void Update () {

Ray ray = Cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
aimtarget.point = ray.origin + (ray.direction * 200);
if (Cursor.lockState == CursorLockMode.Locked)
rotation = Quaternion.LookRotation(aimtarget.point) * Quaternion.Euler(-98, -0.5f, 0);

LauncherObject.transform.rotation = Quaternion.Slerp(LauncherObject.transform.rotation, rotation, Time.deltaTime*5);

LaunchPos = LauncherObject.transform.position;
LaunchRot = LauncherObject.transform.rotation;

if (Input.GetButton("Fire1") && Cursor.lockState == CursorLockMode.Locked && Time.time > fireCool) {

photonView.RPC ("FireGrenade", PhotonTargets.AllViaServer, LaunchPos, LaunchRot);
fireCool = Time.time + FireDelay;
}

if (Input.GetButtonDown("Fire2"))
photonView.RPC ("DetGrenade", PhotonTargets.AllViaServer);
}

[RPC]
void FireGrenade(Vector3 LaunchPos, Quaternion LaunchRot) {
if (photonView.isMine) {
Grenade = GameObject.Instantiate(Resources.Load("GL_Explosive", typeof(GameObject)), LaunchPos, LaunchRot) as GameObject;
Rigidbody Gbody = Grenade.GetComponent<Rigidbody>();
Gbody.AddRelativeForce(Vector3.down*DischargeForce, ForceMode.Impulse);
GetComponent<AudioSource>().PlayOneShot(FireSound, 1f);

}
}

[RPC]
void DetGrenade() {
if (Grenade != null)
Grenade.GetComponent<GrenadeProjectile>().Explode();
}

}[/code2]

Would be really great if someone could help me out! Thank you!

10/6/15: Code and demo updated. Now built with Unity 5.1.0
11/6/15: Code and demo updated to include nonrelated fixes.
13/6/15: Code and demo updated with some fixes. Bug still present. Please help!

Comments

  • vadim
    Options
    Hi,

    - If you call Explode() on every client then no need for "ExplosionEffect" object to be networked (don't create it with PhotonNetwork.Instantiate).
    - Use networked version of destroy PhotonNetwork,Destroy() instead of Destroy(this.gameObject....) for objects created with PhotonNetwork.Instantiate
    - Check that you do no run game logic (like collision detection causing hits count update) on different clients in parallel. That may lead to game state inconsistency. Run logic once and update others with resulting state.
    I don't know if one of these issues is the reason for your problem. But you need to fix them all anyway
    I'm not sure if these issues are reason for your problem but they should be fixed before
  • Blocker226
    Options
    Ah okay, thanks for highlighting those errors! I'll see to fixing them and update the code and observe the results.
  • Blocker226
    Options
    Changing the destroy method to PhotonNetwork.Destroy prevents the grenades from being destroyed immediately after spawning the explosion prefab. Which, since I've added a method to detonate them prematurely, means I can detonate them 10 times over before they finally get destroyed.

    Also, some grenades still have the problem of not exploding and just disappearing. I've updated both the webplayer demo and the code in the first post, please look through it!
  • Blocker226
    Options
    Can I just do a quick bump, since I'm really stumped as to what's going on?

    Currently, the grenades act normally, with the exception that some still refuse to explode. I've changed the methods from PhotonNetwork.Instantiate to the default methods and used RPCs. Sometimes you still get dud grenades, and it's really confusing.

    Could it be possibly related to how PUN handles clone objects?
  • vadim
    Options
    After you switched from PhotonNetwork.Instantiate to the default methods, PUN usage is limited to RPC call for WeaponGrenadeLauncher.
    Extending GrenadeProjectile from Photon.MonoBehaviour does not make sense in that case. You handle it as plain local object.
    Explosion issue is not PUN related.
    I can only suppose that WeaponGrenadeLauncher.Grenade references wrong object when you explode it (may be another projectile already fired)
  • Blocker226
    Options
    Alright, thanks for the help so far! I'll redirect my issue to the Unity Answers forum, and fix that projectile problem you highlighted.