Rigibody bullet/projectile collision problems
Options
AwesomeX
✭
I'm having some issues properly syncing collisions/damage in Photon.
The problem is, one player shoots a bullet. It *seems* to have the positions properly synced, but when it hits the player. It *sometimes* damages him, and other times removes it before doing anything.
Here's my projectile script.
And here's my shooting script.
And of course, my health script.
I'm just trying to figure out the best way in handling rigidbody based projectiles and their damages.
I was told to try and make the collisions only for the host. But I'm not sure how to do that.
Sorry if you have to go through all my code. I'm just not sure what's useful, and what isn't.
The problem is, one player shoots a bullet. It *seems* to have the positions properly synced, but when it hits the player. It *sometimes* damages him, and other times removes it before doing anything.
Here's my projectile script.
using UnityEngine; using System.Collections; public class PistolProjectileScript : MonoBehaviour { public int damage; public GameObject bloodObj; public AudioClip[] sounds; void OnCollisionEnter(Collision other) { foreach (ContactPoint contact in other.contacts) { if (other.gameObject.tag == "Player" || other.gameObject.tag == "DeadPlayer") { GameObject blood = Instantiate(bloodObj, contact.point, Quaternion.identity) as GameObject; blood.audio.clip = sounds[Random.Range(0,sounds.Length)]; blood.audio.Play(); } } if (other.gameObject.tag == "Player") { if(PhotonNetwork.isMasterClient) { PlayerHealthScript playerHealth = other.gameObject.GetComponent<PlayerHealthScript> (); playerHealth.currentHealth -= damage; PhotonNetwork.Destroy (gameObject); } } else { if(PhotonNetwork.isMasterClient) { PhotonNetwork.Destroy (gameObject); } } } }
And here's my shooting script.
using UnityEngine; using System.Collections; public class PistolShootingScript : Photon.MonoBehaviour { public GameObject flash; public float projectileSpeed; // Firerate private float nextFire; public float fireRate = 0.05f; // Ammo private int currentAmmo; public int maxAmmo; // Reloading private bool reloading = false; public float reloadTime; // Sounds public AudioClip gunShot; void Awake () { currentAmmo = maxAmmo; } void Update () { if (Input.GetButtonDown ("Fire1") && Time.time > nextFire) { if(!reloading) { if (currentAmmo > 0) { GameObject clone = PhotonNetwork.Instantiate ("PistolBullet", transform.position, transform.rotation, 0) as GameObject; clone.rigidbody.AddForce(-transform.up * projectileSpeed); photonView.RPC ("FireOneShot", PhotonTargets.All, null); } } } // Weapon Reload if (Input.GetKeyDown(KeyCode.R)) { if (!reloading) { if(currentAmmo < maxAmmo) { reloading = true; Invoke("Reload", reloadTime); } } } } // Fire off a round! [RPC] void FireOneShot() { audio.PlayOneShot(gunShot); flash.SetActive(true); Invoke("flashDisable", 0.05f); nextFire = Time.time + fireRate; currentAmmo --; } void Reload() { currentAmmo = maxAmmo; reloading = false; } void flashDisable () { flash.SetActive (false); } }
And of course, my health script.
using UnityEngine; using System.Collections; public class PlayerHealthScript : Photon.MonoBehaviour { public int currentHealth; public int maxHealth; private bool dead = false; private GameObject myDeadPlayer; public GameObject focusPoint; public Vector3[] respawnPoints; void Start () { currentHealth = maxHealth; } void Update () { if (currentHealth <= 0 && dead == false) { myDeadPlayer = PhotonNetwork.InstantiateSceneObject("DeadPlayer", transform.position, Quaternion.identity, 0, null); myDeadPlayer.rigidbody.velocity = rigidbody.velocity; photonView.RPC ("Death", PhotonTargets.AllBuffered, null); } } void OnGUI() { GUI.Label(new Rect(10, 10, 100, 20), "Health: " + currentHealth); } [RPC] void Death () { dead = true; currentHealth = 0; gameObject.SetActive (false); if (photonView.isMine) { SmoothFollowScript camScript = Camera.main.GetComponent<SmoothFollowScript> (); camScript.target = myDeadPlayer.transform; } Invoke ("GetReadyToRespawn", 5); } void GetReadyToRespawn () { photonView.RPC ("Respawn", PhotonTargets.AllBuffered, null); } [RPC] void Respawn () { dead = false; currentHealth = maxHealth; gameObject.SetActive (true); transform.position = respawnPoints[Random.Range(0,respawnPoints.Length)]; if (photonView.isMine) { SmoothFollowScript camScript = Camera.main.GetComponent<SmoothFollowScript> (); camScript.target = focusPoint.transform; } } }
I'm just trying to figure out the best way in handling rigidbody based projectiles and their damages.
I was told to try and make the collisions only for the host. But I'm not sure how to do that.
Sorry if you have to go through all my code. I'm just not sure what's useful, and what isn't.
0
Comments
-
I've solved my problem, thanks anyway.0
-
Sorry for the late reaction.
Thanks for letting us know. Can you elaborate on what you did?0