Hello everyone, I'm trying to get nice shooting with Photon. I made "Projectile Shooting", it is a cannon and 2 players shoots each other, left/right direction. How can I sync Missile Bullet throwing position over network? I fully didn't understand how works Photon RPC, I have followed several tut and tried to use OnPhotonSerializeView. But I couldn't get nice and exact same sooting over network. Where I have to put PhotonViewComponent? Do I need PhotonView component to "Bullet" prefab? Right now bullet component has Photonview component and its observes Rigidbody. But also its not smooth, bullet appears in the middle of the screen via network view, local view is good. When I observe bullet with OnPhotonSerializeView, local ok but via network missile bullet falls right middle center.
_FIRE SCRIPT
_BULLET SCRIPT
_FIRE SCRIPT
public Transform bullet;
public Transform Duo;
public bool allowShoot;
public GameObject BulletSpawnName;
public int BulletForce = 2000;
public float ShotDelay;
public GameObject Cannon;
public Animator CannonAnimator;
public ParticleSystem muzzleFlash;
void Start ()
{
allowShoot = true;
if (PhotonNetwork.isMasterClient)
{
BulletSpawnName = GameObject.FindWithTag ("B1");
}
if (!PhotonNetwork.isMasterClient)
{
BulletSpawnName = GameObject.FindWithTag ("B2");
}
}
public void Shoot2 ()
{
if (allowShoot == true)
{
Vector3 bulletpos = BulletSpawnName.transform.position;
GameObject Bullet = PhotonNetwork.Instantiate ("Bullet", bulletpos, Quaternion.identity, 0);
Bullet.GetComponent<Rigidbody> ().AddForce (transform.forward * BulletForce);
//Bullet.GetComponent<Rigidbody>().velocity = new Vector3(50, 0, 0);
//Bullet.GetComponent<Rigidbody> ().useGravity = true;
muzzleFlash.Play ();
//Bullet.GetComponent<PhotonView>().RPC("RpcAddForceOnAll", PhotonTargets.AllBuffered);
StartCoroutine ("DoTheDance");
}
}
public IEnumerator DoTheDance()
{
allowShoot = false;
CannonAnimator.SetBool("Shot", true);
yield return new WaitForSeconds(ShotDelay); // waits 1 seconds
allowShoot = true;
CannonAnimator.SetBool("Shot", false);
}
_BULLET SCRIPT
using UnityEngine;
using System.Collections;
public class Bullet : Photon.MonoBehaviour {
public Transform Sparks; //переменная для префаба искр
public GameObject Fire; //переменная для префаба огня
private float LifeTime = 3;//переменная времени жизни пули
private float RespawnTime = 0;//переменная нужная для таймаута
public int RandomBullet;
private float _moveSpeed = 40f;
Vector3 realPosition = Vector3.zero;
Quaternion realRotation = Quaternion.identity;
/*void Start ()
{
RandomBullet = Random.Range (1, 4);
if (RandomBullet == 1)
{
//Fire.SetActive (true);
}
}*/
void OnCollisionEnter(Collision collision)//если объект с этим скриптом сталкивается с коллизией
{
if (collision.gameObject.tag != "Wall")
{
Dead ();//вызываем функцию dead
}
if(RandomBullet == 1)
{
foreach (ContactPoint contact in collision.contacts) {//в точке столкновения
Instantiate (Sparks, transform.position, Quaternion.identity);//создаём префаб искр
}
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext (transform.position);
stream.SendNext (transform.rotation);
}
else
{
transform.position = (Vector3)stream.ReceiveNext ();
transform.rotation = (Quaternion)stream.ReceiveNext ();
}
}
void Start ()
{
RespawnTime += Time.deltaTime; //RespawnTime увеличивается с каждым кадром после создания объекта
if(RespawnTime>LifeTime) // если RespawnTime больше LifeTime
{
Dead();//вызываем функцию dead
}
transform.Translate (0, 0, _moveSpeed * Time.deltaTime);
transform.position = new Vector3 (transform.position.x, transform.position.y, 0);
//transform.position = new Vector3 (realPosition.x, realPosition.y, 0);
//transform.position = Vector3.Lerp(transform.position, realPosition, 0.1f);
//transform.rotation = Quaternion.Lerp(transform.rotation, realRotation, 0.1f);
}
void Dead() // функция dead
{
Destroy(gameObject); //удаляем объект на котором висит скрипт
}
}
I'M sorry for this long scripts. I was experimenting that which one will work good for me. Please need Help. Other moving, animating cannon works fine. Thanks in advance.