instantiating object problem!

Options
I have been following couple of tutorials and trying to make a game according to my taste, unfortunetly i have been stuck at a point since one week.

i am spawning a player without any problem and each spawned clone can control its own player.

the problem is when it comes to shooting!
i am instantiating a sphere prefab(using it as bullet) and when run as single player i dont get any problems but when the second player connects the instantiated ball only appears on the screen of the player who instantiated it

i have tryed attaching photon viev to to sphere prefab and set it to observe but dint really help

i have used this line of code but i am having an error for it

Temporary_Bullet_Handler = PhotonNetwork.Instantiate("Bullet", Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject;

anyone has been succesfull at instantiating objects as bullets ?
are there any tutorials around where i can get some help from about instatiating object on network ?
and does instatiating such object brings too much load on netwrok?

thanks for the help in advence

Comments

  • jeanfabre
    Options
    Hi,

    If your bullet is a "missile" that will fly for several seconds, then yeah, you should instantiate it using a prefab over the network, but if it's almost instant, a better option would be to simply send an RPC call about the fire.

    Is your bullet a proper Networked Prefab like your player it? it must be in the Resources folder and have a photonView if you want it to be instantiated over the network.

    do you get any errors in the console? to test this, run one published version and one within the Unity Editor, then you can debug quicker what might be wrong.

    Bye,

    Jean
  • Zozan
    Options
    First of all thanks for asisting me on this project Jean Fabre, i will definetly give you credits if i ever manage to fnish it, to be honest i was about to give up today!

    my bullets are actually small bouncy balls that stays in the scene for 4 second and disappear, bullets are very bouncy and can harm the player or opponent when shooting around

    I have placed all my prefabs in my resources forder

    the only error i get is "Assets/Scripts/NetworkManager.cs(54,20): warning CS0219: The variable `Plyr' is assigned but its value is never used" and it does not bother me or my game at all (i have posted the code below maybe you know a solution for it)

    i have tested my game the way you pointed above

    ı have no experience with RPC calls (my experience with unity is no more then 1 weeks and i have no experience with coding but i am starting to understand it slowly ) is there a good tutorial that can help me out around about RPC?

    here is my code for network manager


    [CODE]
    using UnityEngine;
    using System.Collections;
    
    public class NetworkManager : MonoBehaviour {
        public Transform[] SpawnNoktalari;
        public bool offlineMode = false;
     
    
        // Use this for initialization
        void Start () {
    
            Connect();
          
    
        }
    	
    	
    	void Connect () {
            if(offlineMode)
            {
                PhotonNetwork.offlineMode = true;
                OnJoinedLobby();
            }
            else
            {
    
                PhotonNetwork.ConnectUsingSettings ("closed beta version 1");
            
             }
        }
        void OnGUI (){
            GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString());
            }
    
        void OnJoinedLobby (){
            Debug.Log ("OnJoinedLobby");
            PhotonNetwork.JoinRandomRoom();
            }
        void OnPhotonRandomJoinFailed(){
            Debug.Log ("OnPhotonRandomJoinFailed");
            PhotonNetwork.CreateRoom(null);
            }
        void OnJoinedRoom (){
            Debug.Log("OnJoinedRoom");
            Spawn();
            }
    
        void Spawn(){
    
    
            int spawnnoktasi = Random.Range(0, SpawnNoktalari.Length);
    
    
            GameObject Plyr = (GameObject) PhotonNetwork.Instantiate("Player", SpawnNoktalari [spawnnoktasi].position,Quaternion.identity,0);
    
    
      
           
    
        }
    }
    [/CODE]



    This script here is the one i attach to Photon View to track my player movement



    [CODE]
    using UnityEngine;
    using System.Collections;
    
    public class NetworkCharacter : Photon.MonoBehaviour {
    
        Vector3 realPosition = Vector3.zero;
        Quaternion realRotation = Quaternion.identity;
        Animator anim;
       
        void Start () {
            anim = GetComponent<Animator>();
    	
    	}
    	
    	// Update is called once per frame
    	void Update () {
    	  if ( photonView.isMine)
            {
    
            }
          else
            {
                transform.position = Vector3.Lerp(transform.position, realPosition, 10f * Time.deltaTime); 
                transform.rotation = Quaternion.Lerp(transform.rotation, realRotation, 10f * Time.deltaTime);
            }
        }
    
        void OnPhotonSerializeView (PhotonStream stream, PhotonMessageInfo info)
        {
            if (stream.isWriting){
    
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation);
            stream.SendNext(anim.GetBool("IsWalking"));
            stream.SendNext(anim.GetBool("Die"));
    
    
            }
            else {
    
           realPosition = (Vector3)stream.ReceiveNext();
           realRotation = (Quaternion)stream.ReceiveNext();
           anim.SetBool("IsWalking",(bool)stream.ReceiveNext());
           anim.SetBool("Die", (bool)stream.ReceiveNext());
            }
    }
    }
    

    [/CODE]



    this script is the one i use for shooting and its attached to my player as component


    [CODE]
    using UnityEngine;
    using System.Collections;
    
    public class PlayerShooting : MonoBehaviour
    {
    
        
        
        public GameObject Bullet_Emitter;
    
       
        public GameObject Bullet;
    
        
        public float Bullet_Forward_Force;
    
    
    
        void Start()
        {
         
    
        }
    
        
        void Update()
        {
    
    
            if (Input.GetMouseButtonDown(0))
            {
    
               
              
                GameObject Temporary_Bullet_Handler;
                Temporary_Bullet_Handler = Instantiate(Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject;
    
                
                Temporary_Bullet_Handler.transform.Rotate(Vector3.left * 90);
    
                
                Rigidbody Temporary_RigidBody;
                Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
    
                
                Temporary_RigidBody.AddForce(transform.forward * Bullet_Forward_Force);
    
                
                Destroy(Temporary_Bullet_Handler, 4.0f);
    
             
              
                }
    
    
            }
        public void DisableEffects()
        {
            
            Desroy(this);
        }
    
    }
    
    
    [/CODE]


    and here is the last part a picture of my game those red balls are what i am trying to spawn over the network



  • jeanfabre
    Options
    Hi,

    Don't worry about the credits :) And make sure you never give up. This is your biggest challenge in this kind of project development to overcome the hurdles and find a way into all these issues and learning curves.

    A Warning is not an Error, this is very important, so in the Unity console, if it's yellow, it's not that bad, Only Red logs are errors and should absolutly be addressed unless stated as a know bug and harmless ( very very rare).

    Glancing through your code, I can see some typos, so I doubt any of it works currently. You'll need to clean up them scripts until you have no errors in Unity console after compilation. typically " Desroy(this);" should be " Destroy(this);"

    Also, I don't see where you are actually controlling your character using Unity Input.


    Then yes, your bouncy Spheres should be instantiated. If I understand well they don't belong to any characters right? in that case, you can use Scene Objects: https://doc.photonengine.com/en/pun/current/tutorials/instantiation

    Rpc: Check this video: https://youtu.be/ozBmZ9FoN_o Oliver has extensive experience with Photon, so all his stuff is a must watch really. Careful tho, some minor change in scripts have been made, so the code slightly changes from place to place, but it's minor, always fallback on the various Demos provided in the Photon Package to have the latest scripts

    Since you are starting with both Unity and Photon. I strongly recommend that you first go through a pahse of learning using not necessarly what you want to achieve as a goal, but simply for the sake of learning. When you'll have acquired sufficient confidence in yourskills, then you should tackle your own projects with original gameplays and all. Don't try to achieve your ultimate game straight away, it's a journey... :)

    Your level and graphics looks very nice btw!

    Bye,

    Jean

  • Zozan
    Options
    thanks for the help!

    the game works fine at the moment besides the part that i cant instantiate the sphere on server / typos are only here, for some reason when i paste the code i had smilies everywhere so i deleted them by hand looks like at some parts i deleted a bit more then i have inteded

    ı have watched all the video series but cant say i have understand much, i will try to figure out a way thanks for the help again
  • jeanfabre
    Options
    Ok.

    Keep at it, and try to instanciate theses scene objects.

    If you get stuck, get back to me, and explain what doesn't work, we'll move forward.

    Bye,

    Jean
  • Zozan
    Options
    ALlright i have tryed to edit the code a bit but looks like with this new code i cant even fire on local player, i have attached a photon view to bullet prefeb and put its transform to observe, here is the code any idea what is wrong with it ?
    using UnityEngine;
    using System.Collections;
    public class PlayerShooting : Photon.MonoBehaviour
    {
    
        
        //Drag in the Bullet Emitter from the Component Inspector.
        public GameObject Bullet_Emitter;
    
        //Drag in the Bullet Prefab from the Component Inspector.
        public GameObject Bullet;
    
        //Enter the Speed of the Bullet from the Component Inspector.
        public float Bullet_Forward_Force;
    
      
    
        void Start()
        {
            
            
        }
        // Update is called once per frame
       
        void Update()
        { }
    
    
        [PunRPC]
        void RPCSHOOT()
        {
    
            if (Input.GetMouseButtonDown(0))
            {
    
              
    
                GameObject Temporary_Bullet_Handler;
                Temporary_Bullet_Handler = Instantiate(Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject;
    
            
               
                Temporary_Bullet_Handler.transform.Rotate(Vector3.left * 90);
          
                Rigidbody Temporary_RigidBody;
                Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
    
                
                Temporary_RigidBody.AddForce(transform.forward * Bullet_Forward_Force);
    
            
                Destroy(Temporary_Bullet_Handler, 4.0f);
    
               
                GetComponent<PhotonView>().RPC("RPCSHOOT", PhotonTargets.AllBuffered);
            }
        }
        public void DisableEffects()
        {
    
            Destroy(this);
        }
    
    }
  • jeanfabre
    Options
    Hi,

    yes, first of all:

    you need to move the Input management inside the Update method, not inside the RPC. because what's going to happen is that as other instance receive the rpc they will fail with the input management.

    SO, inside Update, you need to first check if PhotonView.isMine is true and if it is, you are entitled to watch for input and fire an RPC call if necessary.

    Bye,

    Jean