- Home /
Sync Projectile Shooting with Photon Networking
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
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.
Really nobody doesn't knows photon projectile shooting? There is a project calls "Angry Bots" photon has integrated. There is "AutoFire.js script and its not easy to understand that because code has connected with huge numbers of scripts. And of course all codes in JavaScript(((
Hi! Did you manage to solve this problem? I am expierencing exactly the same issue now and I would really need some help :)
Select your bullet prefab and attach a PhotonView component as well as a PhotonTransformView to it. In the PhotonTransformView make sure that 'Synchronize Position' and 'Synchronize Rotation' are checked. Drag and drop the PhotonTransformView onto the observed components of the PhotonView. You don't need to manually send and/or receive position or rotation data.
i am leaving sending data on your shoulders, the way it can be done is by sending velocity and position.
if the initial-Position and initial-velocity of any 2 object is same then they both will follow the exact same trajectory.
when you shoot on one client send it's position and velocity (after adding force) to all other clients and leave it on Physx.
Your answer
![](https://koobas.hobune.stream/wayback/20220612075317im_/https://answers.unity.com/themes/thub/images/avi.jpg)