2
\$\begingroup\$

In older unity versions you could simply used RPC. With newer verisons of Unity my code now doesn't work and it says that it needs to be transformed into "PunRPC" , Pun being "Photon Unity Networking." I am not sure how to reformat it to work with current unity versions. Can anyone shed light on this?

My Code

FXManager.cs

1 using UnityEngine;  
2 using System.Collections;  
3  
4 public class FXManager : MonoBehaviour {  
5  
6   [RPC]  
7   void SniperBulletFX( Vector3 startPos, Vector3 endPos ) {  
8       Debug.Log ("SniperBulletFX");  
9  
10  }  
11}  

PlayerShooting.cs

1 using UnityEngine;
2    using System.Collections;
3
4    public class PlayerShooting : MonoBehaviour {
5
6   public float fireRate = 0.5f;
7   float cooldown = 0;
8   public float damage = 25f;
9   FXManager fxManager;
10
11  void Start() {
12      fxManager = GameObject.FindObjectOfType<FXManager>();
13
14      if(fxManager == null) {
15          Debug.LogError("Couldn't find a FXManger.");
16      }
17  }
18
19
20  // Update is called once per frame
21  void Update () {
22      cooldown -= Time.deltaTime;
23
24      if (Input.GetButton ("Fire1")) {
25          //Player wants to shoot...so. Shoot.
26          Fire ();
27      }
28  }
29
30  void Fire() {
31
32      if (cooldown > 0) {
33          return;
34      }
35
36      Debug.Log ("Firing our gun");
37
38      Ray ray = new Ray (Camera.main.transform.position, Camera.main.transform.forward);
39      Transform hitTransform;
40      Vector3 hitPoint;
41
42      hitTransform = FindClosestHitObject (ray, out hitPoint);
43
44      if (hitTransform != null) {
45          Debug.Log ("We hit:" + hitTransform.name);
46
47          // We could do a special effect at the hit location
48          // DoRicochetEffectAt (hitPoint);
49
50          Health h = hitTransform.GetComponent<Health> ();
51
52          while (h == null && hitTransform.parent) {
53              hitTransform = hitTransform.parent;
54              h = hitTransform.GetComponent<Health> ();
55          }
56
57          // Once we reach here, hitTransform may not be the hitTransform we started with!
58
59          if (h != null) {
60              // This next line is the equivalent of calling;
61              //                  h.takeDamage( damage);
62              //Except more "networky"
63              PhotonView pv = h.GetComponent<PhotonView> ();
64              if (pv == null) {
65                  Debug.LogError ("Freak out!");
66              } else {
67                  h.GetComponent<PhotonView> ().RPC ("TakeDamage", PhotonTargets.AllBuffered, damage);
68              }
69          }
70
71          if (fxManager != null) {
72              fxManager.GetComponent<PhotonView>().RPC ("SniperBulletFx", PhotonTargets.All, Camera.main.transform.position, hitPoint);
73          }
74      }
75      else {
76          // We didn't hit anything (except empty space), but let's do a visual FX anyway
77          if (fxManager != null) {
78              hitPoint = Camera.main.transform.position + (Camera.main.transform.forward*100f);
79              fxManager.GetComponent<PhotonView>().RPC ("SniperBulletFx", PhotonTargets.All, Camera.main.transform.position, hitPoint);
80          }
81      }
82
83      cooldown = fireRate;
84  }

If anymore information is needed, please let me know, I would love to figure out how the new "PunRPC(Photon Unity Neworking)" works so I can use it in future projects.

Edit: This is the error I get "PhotonView with ID 2 has no method "SniperBulletFx" marked with the PunRPC or @PunRPC(JS) property! Args: Vector3, Vector3 UnityEngine.Debug:LogError(Object)"

\$\endgroup\$
-2
\$\begingroup\$

You just change [PUN] to [PunRPC].

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.