Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alarm656 · Apr 21, 2016 at 01:57 PM · networkingphotonprojectile

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alarm656 · Apr 24, 2016 at 09:24 PM 0
Share

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(((

avatar image mwozniak93 alarm656 · Aug 17, 2016 at 08:38 PM 0
Share

Hi! Did you manage to solve this problem? I am expierencing exactly the same issue now and I would really need some help :)

avatar image ChristianSimon · Aug 22, 2016 at 12:11 PM 0
Share

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.

avatar image _Yash_ · Aug 22, 2016 at 12:35 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Photon Unity Networking - Projectile Syncing problem 0 Answers

Bullet Collision in Networking Game. 0 Answers

Proper way to instantiate projectile in PUN 1 Answer

Photon (PUN) Type Serialization Error on RPC 2 Answers

Unity array out of range exception, using photon unity network. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges