Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Third Party Photon (possible RPC instantiation of bullet)

Discussion in 'Multiplayer' started by FisherM, Jun 26, 2014.

  1. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    So I have a working single player system where a bullet is instantiated in and applies all its forces to itself.

    Below is the code attached to a bullet emitter on the end of the bore of every gun of every player.
    It is called upon left mouse button down.

    Code (CSharp):
    1. GameObject bulletemitter = GameObject.Find("BulletEmitter");
    2. object_fish_gamelogic.REALFIRE(myPhotonView, ProjectilePrefab, bulletemitter.transform.position, Quaternion.Euler( bulletemitter.transform.rotation.eulerAngles.x, bulletemitter.transform.rotation.eulerAngles.y, bulletemitter.transform.rotation.eulerAngles.z ));
    inside the fish_GameLogic script I have the following
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class fish_GameLogic : Photon.MonoBehaviour {
    5.     GameObject projectilePrefab;
    6.     Vector3 projectileLocation;
    7.     Quaternion projectileRotation;
    8.    
    9.    
    10.    
    11.     [RPC]
    12.     void RPCSHOOT(){
    13.         Object projecPrefab = Instantiate(projectilePrefab, projectileLocation, projectileRotation);
    14.     }
    15.  
    16.     public void REALFIRE (PhotonView myPhotonView, GameObject projectilePrefab1, Vector3 projectileLocation1, Quaternion projectileRotation1){
    17.         GameObject projectilePrefab = projectilePrefab1;
    18.         Vector3 projectileLocation = projectileLocation1;
    19.         Quaternion projectileRotation = projectileRotation1;
    20.         myPhotonView.RPC("RPCSHOOT", PhotonTargets.All);
    21.     }
    22. }
    23.  
    24.  
    Both the character and the NetworkManager have a photon view.

    Here is the error I get.

     
    Last edited: Jun 26, 2014
  2. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    If there is another way to do this that is fine, however it is important the following is understood.
    The bullets DO NOT need to be synced between clients. I only want a hit on the local (client who fires the bullet) to count as a hit.

    Also, I have read a ton on this and have been trying to fix it for a few days now and don't understand the concept. I have read the marco polo tutorial but it isn't quite transferable to this example.
     
  3. Turbo420

    Turbo420

    Joined:
    Jun 24, 2014
    Posts:
    61
    remove the public in RPCSHOOT();

    void RPCSHOOT();

    i don't think that a rpc can be public. ;)
    hope this help you out.
     
  4. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    tried it same error.
     
  5. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    Also I updated the original post to reflect that change
     
  6. Turbo420

    Turbo420

    Joined:
    Jun 24, 2014
    Posts:
    61
    ok i see it now. you are using two photonviews and they both have different ID's you have to send it and receive it over one photonview not two. Do you get me?
     
  7. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    I understand you but what is the solution?
    Right now the game_logic script is in the network manager game object
    do I need to attach the game_logic script to the player with the photonview?
     
  8. Turbo420

    Turbo420

    Joined:
    Jun 24, 2014
    Posts:
    61
    If there a photonview on the same gameobject as the fish_GameLogic script is on the just do this
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class fish_GameLogic :MonoBehaviour {
    5.     GameObject projectilePrefab;
    6.     Vector3 projectileLocation;
    7.     Quaternion projectileRotation;
    8.  
    9.  
    10.  
    11.     [RPC]
    12.     void RPCSHOOT(){
    13.         Object projecPrefab = Instantiate(projectilePrefab, projectileLocation, projectileRotation);
    14.     }
    15.     public void REALFIRE (GameObject projectilePrefab1, Vector3 projectileLocation1, Quaternion projectileRotation1){
    16.         GameObject projectilePrefab = projectilePrefab1;
    17.         Vector3 projectileLocation = projectileLocation1;
    18.         Quaternion projectileRotation = projectileRotation1;
    19. GetComponent<PhotonView>().RPC ("RPCSHOOT", PhotonTargets.AllBuffered);
    20.    }
    21. }
    22.  
    then call it like this:
    Code (CSharp):
    1.     GameObject bulletemitter = GameObject.Find("BulletEmitter");
    2.     object_fish_gamelogic.REALFIRE( ProjectilePrefab, bulletemitter.transform.position, Quaternion.Euler( bulletemitter.transform.rotation.eulerAngles.x, bulletemitter.transform.rotation.eulerAngles.y, bulletemitter.transform.rotation.eulerAngles.z ));
    3.  
     
  9. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    I now get this

    NullReferenceException
    UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineObject.cs:73)
     
  10. Turbo420

    Turbo420

    Joined:
    Jun 24, 2014
    Posts:
    61
    }[/code]
    what is projecPrefab for? you can remove it and call it like this:
    Instantiate(projectilePrefab, projectileLocation, projectileRotation);

    try this and let me know what you get.;)
    Code (CSharp):
    1.  
    2. [RPC]
    3.     void RPCSHOOT(){
    4.         Object projecPrefab = Instantiate(projectilePrefab, projectileLocation, projectileRotation);
    5. if (projectPrefab == null){
    6. Debug.Log("No projectPrefab ");
    7. Debug.Log("but fired over the network ");
    8.  
    9. }
    10.  
    11.    }
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class fish_GameLogic :MonoBehaviour {
    5.     GameObject projectilePrefab;
    6.     Vector3 projectileLocation;
    7.     Quaternion projectileRotation;
    8.  
    9.  
    10.  
    11.     [RPC]
    12.     void RPCSHOOT(){
    13.         Object projecPrefab = Instantiate(projectilePrefab, projectileLocation, projectileRotation);
    14.     }
    15.     public void REALFIRE (GameObject projectilePrefab1, Vector3 projectileLocation1, Quaternion projectileRotation1){
    16.        projectilePrefab = projectilePrefab1;
    17.       projectileLocation = projectileLocation1;
    18.     projectileRotation = projectileRotation1;
    19. GetComponent<PhotonView>().RPC ("RPCSHOOT", PhotonTargets.AllBuffered);
    20.    }
     
    Last edited: Jun 26, 2014
  11. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    There are two problems here.

    1. You need to use a photonview on the object holding the method. Can see you fixed it with GetComponnent<PhotonView>. Just remeber this when designing your network code.

    2. You null reference exception is because you only set the prefab, location and rotation locally in REALFIRE(). Everyone else than the one who is calling the RPC will not have it set, and hence you get the null reference exception.
     
    Turbo420 likes this.
  12. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    so there is some sort of progress. However. the bullet now instantiates based of the character's bullet emitter. As in when another player fires it now instantiates the bullet at my barrel tip.

    How can I have the bullet only instantiate at the BulletEmitter of the player firing?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class fish_GameLogic : Photon.MonoBehaviour {
    5.     GameObject projectilePrefab;
    6.     Vector3 projectileLocation;
    7.     Quaternion projectileRotation;
    8.  
    9.  
    10.  
    11.     [RPC]
    12.     void RPCSHOOT(){
    13.         Object projecPrefab = Instantiate(projectilePrefab, projectileLocation, projectileRotation);
    14.     }
    15.  
    16.     public void REALFIRE (PhotonView myPhotonView, GameObject projectilePrefab1, Vector3 projectileLocation1, Quaternion projectileRotation1){
    17.         projectilePrefab = projectilePrefab1;
    18.         projectileLocation = projectileLocation1;
    19.         projectileRotation = projectileRotation1;
    20.         GetComponent<PhotonView>().RPC("RPCSHOOT", PhotonTargets.All);
    21.     }
    22.  
    23.  
    24. }
     
  13. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    I also get a NullReference if a player shoots before i have shot. Presumeably because the Prefab hasn't been setup yet. This seems to be the wrong way of going about it so how would be the right way?
     
  14. Turbo420

    Turbo420

    Joined:
    Jun 24, 2014
    Posts:
    61
    if(PhotonView.isMine){
    }

    and Object Pooling your bullet
     
  15. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    I can't imagine that is going to help as the issue is already that the object fires from my gun even if it is someone else shooting
     
  16. Turbo420

    Turbo420

    Joined:
    Jun 24, 2014
    Posts:
    61
    ok lets start by fixing the error.
    Code (CSharp):
    1.  
    2.   using UnityEngine;
    3.     using System.Collections;
    4.  
    5.     public class fish_GameLogic : Photon.MonoBehaviour {
    6. //now drop the prefab on the script  
    7. public GameObject projectilePrefab;
    8.  
    9.  
    10.  
    11.  
    12.         [RPC]
    13.         void RPCSHOOT( Vector3 projectileLocation, Quaternion projectileRotation){
    14.             Object projecPrefab = Instantiate(projectilePrefab, projectileLocation, projectileRotation);
    15.         }
    16.  
    17.         public void REALFIRE ( Vector3 projectileLocation1, Quaternion projectileRotation1){
    18.    
    19.             GetComponent<PhotonView>().RPC("RPCSHOOT", PhotonTargets.All, projectileLocation1,projectileRotation1 );
    20.         }
    21.  
    22.  
    23.     }
    24.  
     
  17. Turbo420

    Turbo420

    Joined:
    Jun 24, 2014
    Posts:
    61
    try that;) this should remove the error but will still fire from both players
     
    Last edited: Jun 27, 2014
  18. rudyx

    rudyx

    Joined:
    Jun 14, 2016
    Posts:
    3
    Probably you forgot to made it thru PhotonNetwork
    Object projecPrefab = PhotonNetwork.Instantiate(projectilePrefab, projectileLocation, projectileRotation);
     
  19. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    Thanks, swapped to forge networking