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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Instantiating Prefabs

Discussion in 'Multiplayer' started by iEntity, Mar 19, 2015.

  1. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    So In my multiplayer game that I am making (with Photon) I have my player instantiate a prefab every time he wants to shoot. Every time my player shoots all of the players on the network instantiate the prefab. How do I make only the person who clicked the button shoot and not everyone (but they can still see the bullet)? Also my prefab is coming out sideways.. Do you know how I can fix that? (I have tried rotating the actual prefab and even the spawn position)


    Code (CSharp):
    1.    
    2.  void DartEffect ( Vector3 startPos, Vector3 endPos)
    3.     {
    4.         Debug.Log ("DartFX");
    5.  
    6.         GameObject dartFX = (GameObject) Instantiate (dartFxPrefab, startPos, Quaternion.LookRotation( endPos - startPos ));
    7.     }
    8.  
    9.  
    10.  
    11.  
    12.     void Update ()
    13.     {
    14.         cooldown -= Time.deltaTime;
    15.        
    16.         if (Input.GetButtonDown("Fire1"))
    17.         {
    18.             Throw ();
    19.         }
    20.     }
    21.  
    22.  
    23.  
    24.  
    25. void Throw ()
    26.     {
    27.         if (cooldown > 0)
    28.         {
    29.             return;
    30.         }
    31.         Debug.Log ("Firing Our Gun");
    32.        
    33.         Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
    34.         Transform hitTransform;
    35.         Vector3 hitPoint;
    36.  
    37.         hitTransform = FindClosestHitObject(ray, out hitPoint);
    38.        
    39.         if (hitTransform != null)
    40.         {
    41.             Debug.Log ("We hit: " + hitTransform.name);
    42.            
    43.             Health h = hitTransform.GetComponent<Health>();
    44.            
    45.             while(h == null && hitTransform.parent)
    46.             {
    47.                 hitTransform = hitTransform.parent;
    48.                 h = hitTransform.GetComponent<Health>();
    49.             }
    50.            
    51.             if(h != null)
    52.             {
    53.                 PhotonView pv = h.GetComponent<PhotonView>();
    54.                 if (pv == null)
    55.                 {
    56.                     Debug.LogError ("Freak Out");
    57.                 }
    58.                 else
    59.                 {
    60.                     h.GetComponent<PhotonView>().RPC ("TakeDamage", PhotonTargets.All, damage);
    61.                    
    62.                 }
    63.             }
    64.             if (fxManager != null)
    65.             {
    66.                 DoGunFX(hitPoint);
    67.             }
    68.             else
    69.             {
    70.                 if (fxManager != null)
    71.                 {
    72.                     hitPoint = Camera.main.transform.position + (Camera.main.transform.forward*100f);
    73.                     DoGunFX(hitPoint);
    74.                 }
    75.             }
    76.         }
    77.        
    78.         cooldown = fireRate;
    79.        
    80.     }
    81.  
    82.  
    83.  
    84.  
    85.  
     
  2. nostalgicbear

    nostalgicbear

    Joined:
    Mar 21, 2013
    Posts:
    98
    Where you are declaring your class, add "Photon.MonoBehaviour". So for example :

    Code (CSharp):
    1. public class NetworkCharacter : Photon.MonoBehaviour
    2. {
    3. //All your code
    4. }
    When you are pressing the button to shoot or instantiate the object, you should probably check to see i you control the player before you instantiate the object, so then if you dont own the player controller, you wont instantiate the object for that player.

    I assume that seeing as this is a multiplayer game, you are using Photon. So where you are are pressing the button, do a check to first see if you own the player.

    Code (CSharp):
    1. if(photonView.isMine)
    2. {
    3. if (Input.GetButtonDown("Fire1"))
    4.         {
    5.             Throw ();
    6.         }
    7. }
    If you are not using photon you can just do :

    Code (CSharp):
    1. if(networkView.isMine)
    As for spawning the bullet, have an empty game object as the spawn position and have its rotation values set to 0,0,0. Then when you spawn the bullet, spawn it at that empty spawn objects position using its transform and rotation.

    Hope this helps :)
     
  3. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    Thanks you man, you are a lifesaver!

    It worked!! Thank you for your time and help!
     
  4. nostalgicbear

    nostalgicbear

    Joined:
    Mar 21, 2013
    Posts:
    98
    No problem, glad it helped. :)