Search Unity

A Merry Fragmas Update v5

Discussion in 'Community Learning & Teaching' started by ronbonomo, Nov 2, 2015.

  1. ronbonomo

    ronbonomo

    Joined:
    Oct 15, 2015
    Posts:
    32
    This is for version 5.2 using version 5 Standard assets/fpscontroller. Just follow his video with the scripts I provide here and side notes I give.While working through this tutorial I had to pay extra attention to Mike Geig Unity set up very carefully to solve some issues. Like playing that game, what is different in this picture lol.

    Photon: You wont find much on photon in unity forums. The photon site is were you need to go to find your solutions.With the new photon, after you put in you apiid the settings will pop up in your inspector. In the video Mike does nothing to it and moves on with the project. Well now you have two boxes that by default are unchecked. One is Auto log into Lobby, the other is to allow lobby stat. Be sure you check the Auto join lobby, or you wont connect to the lobby using this tutorial. The second is in how RPC is called. Now you have to put PunRPC. But that issue will correct itself, you just have to allow it to when the option pops up. And is already corrected in these scripts. Just future knowledge.

    Lets move on. If you have sync issues, make sure once you write your PlayerNetworkMover script that you add that to your PhotonView to observe. Not doing so will cause you to not sync and through errors. Don't forget that step. Now when you see the script I post, you will notice I commented out all the animation stuff. That is because I used a humanoid with more complex animations. I handle animation sync differently with photonAnimationView. Makes syncing complex animations much easier. Again go to photon site to learn more on there Mecanim sync. And you will notice I have an additional call for turning on audio. That is because I have a few audio sources on my character such as fire sound. Which I added in the Shooting script. All you have to do is add an audio source to your gun camera then add your shooting sound to it.By following Mike in this tutorial, using the notes, and the scripts here, you should be up and going with this tutorial. If you have any other issues which I did not cover here, please feel free to contact me and I will be happy to help you find a solution. CHEERS!!!!

    Network Manager

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System.Collections.Generic;
    5.  
    6. public class NetworkManager : MonoBehaviour
    7. {
    8.     [SerializeField] Text connectionText;
    9.     [SerializeField] Transform[] spawnPoints;
    10.     [SerializeField] Camera sceneCamera;
    11.    
    12.     [SerializeField] GameObject serverWindow;
    13.     [SerializeField] InputField username;
    14.     [SerializeField] InputField roomName;
    15.     [SerializeField] InputField roomList;
    16.     [SerializeField] InputField messageWindow;
    17.    
    18.     GameObject player;
    19.     Queue<string> messages;
    20.     const int messageCount = 6;
    21.     PhotonView photonView;
    22.    
    23.    
    24.     void Start ()
    25.     {
    26.         photonView = GetComponent<PhotonView> ();
    27.         messages = new Queue<string> (messageCount);
    28.        
    29.         PhotonNetwork.logLevel = PhotonLogLevel.Full;
    30.         PhotonNetwork.ConnectUsingSettings ("1.0");
    31.         StartCoroutine ("UpdateConnectionString");
    32.     }
    33.    
    34.     IEnumerator UpdateConnectionString ()
    35.     {
    36.         while(true)
    37.         {
    38.             connectionText.text = PhotonNetwork.connectionStateDetailed.ToString ();
    39.             yield return null;
    40.         }
    41.     }
    42.    
    43.     void OnJoinedLobby()
    44.     {
    45.         serverWindow.SetActive (true);
    46.     }
    47.    
    48.     void OnReceivedRoomListUpdate()
    49.     {
    50.         roomList.text = "";
    51.         RoomInfo[] rooms = PhotonNetwork.GetRoomList ();
    52.         foreach(RoomInfo room in rooms)
    53.             roomList.text += room.name + "\n";
    54.     }
    55.    
    56.     public void JoinRoom()
    57.     {
    58.         PhotonNetwork.player.name = username.text;
    59.         RoomOptions roomOptions = new RoomOptions(){ isVisible = true, maxPlayers = 10 };PhotonNetwork.JoinOrCreateRoom
    60.             (roomName.text, roomOptions, TypedLobby.Default);
    61.     }
    62.    
    63.     void OnJoinedRoom()
    64.     {
    65.         serverWindow.SetActive (false);
    66.         StopCoroutine ("UpdateConnectionString");
    67.         connectionText.text = "";
    68.         StartSpawnProcess (0f);
    69.     }
    70.    
    71.     void StartSpawnProcess(float respawnTime)
    72.     {
    73.         sceneCamera.enabled = true;
    74.         StartCoroutine ("SpawnPlayer", respawnTime);
    75.     }
    76.    
    77.     IEnumerator SpawnPlayer(float respawnTime)
    78.     {
    79.         yield return new WaitForSeconds(respawnTime);
    80.        
    81.         int index = Random.Range (0, spawnPoints.Length);
    82.         player = PhotonNetwork.Instantiate("FPSPlayer", spawnPoints[index].position, spawnPoints[index].rotation, 0);
    83.         player.GetComponent<PlayerNetworkMover> ().RespawnMe += StartSpawnProcess;
    84.         player.GetComponent<PlayerNetworkMover> ().SendNetworkMessage += AddMessage;
    85.         sceneCamera.enabled = false;
    86.        
    87.         AddMessage ("Spawned player: " + PhotonNetwork.player.name);
    88.     }
    89.    
    90.     void AddMessage(string message)
    91.     {
    92.         photonView.RPC ("AddMessage_RPC", PhotonTargets.All, message);
    93.     }
    94.    
    95.     [PunRPC]
    96.     void AddMessage_RPC(string message)
    97.     {
    98.         messages.Enqueue (message);
    99.         if(messages.Count > messageCount)
    100.             messages.Dequeue();
    101.        
    102.         messageWindow.text = "";
    103.         foreach(string m in messages)
    104.             messageWindow.text += m + "\n";
    105.     }
    106. }
    PlayerNetworkMover

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerNetworkMover : Photon.MonoBehaviour
    5. {
    6.     public delegate void Respawn(float time);
    7.     public event Respawn RespawnMe;
    8.     public delegate void SendMessage(string MessageOverlay);
    9.     public event SendMessage SendNetworkMessage;
    10.    
    11.     Vector3 position;
    12.     Quaternion rotation;
    13.     float smoothing = 10f;
    14.     float health = 100f;
    15.     //bool aim = false;
    16.     //bool sprint = false;
    17.     bool initialLoad = true;
    18.    
    19.     //Animator anim;
    20.    
    21.     void Start()
    22.     {
    23.         //anim = GetComponentInChildren<Animator> ();
    24.         if(photonView.isMine)
    25.         {
    26.             GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().enabled = true;
    27.             GetComponent<Rigidbody> ().useGravity = true;
    28.             GetComponent<CharacterController> ().enabled = true;
    29.             GetComponentInChildren<Shooting> ().enabled = true;
    30.             GetComponentInChildren<AudioListener> ().enabled = true;
    31.             GetComponentInChildren<AudioSource> ().enabled = true;
    32.            
    33.             foreach(AudioSource audio in GetComponentsInChildren<AudioSource> ())
    34.                 audio.enabled = true;
    35.  
    36.             foreach(Camera cam in GetComponentsInChildren<Camera>())
    37.                 cam.enabled = true;
    38.            
    39.             foreach(Camera cam in GetComponentsInChildren<Camera>())
    40.                 cam.enabled = true;
    41.         }
    42.         else
    43.         {
    44.             StartCoroutine("UpdateData");
    45.         }
    46.     }
    47.    
    48.     IEnumerator UpdateData ()
    49.     {
    50.         if(initialLoad)
    51.         {
    52.             initialLoad = false;
    53.             transform.position = position;
    54.             transform.rotation = rotation;
    55.         }
    56.        
    57.         while(true)
    58.         {
    59.             transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * smoothing);
    60.             transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * smoothing);
    61.             //anim.SetBool("Aim", aim);
    62.             //anim.SetBool ("Sprint", sprint);
    63.             yield return null;
    64.         }
    65.     }
    66.    
    67.     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    68.     {
    69.         if (stream.isWriting)
    70.         {
    71.             stream.SendNext(transform.position);
    72.             stream.SendNext(transform.rotation);
    73.             stream.SendNext(health);
    74.             //stream.SendNext(anim.GetBool ("Aim"));
    75.             //stream.SendNext(anim.GetBool ("Sprint"));
    76.         }
    77.         else
    78.         {
    79.             position = (Vector3)stream.ReceiveNext();
    80.             rotation = (Quaternion)stream.ReceiveNext();
    81.             health = (float)stream.ReceiveNext();
    82.             //aim = (bool)stream.ReceiveNext();
    83.             //sprint = (bool)stream.ReceiveNext();
    84.         }
    85.     }
    86.    
    87.     [PunRPC]
    88.     public void GetShot(float damage, string enemyName)
    89.     {
    90.         health -= damage;
    91.        
    92.         if (health <= 0 && photonView.isMine){
    93.            
    94.             if(SendNetworkMessage != null)
    95.                 SendNetworkMessage(PhotonNetwork.player.name + " was killed by " + enemyName);
    96.            
    97.             if(RespawnMe != null)
    98.                 RespawnMe(3f);
    99.            
    100.             PhotonNetwork.Destroy (gameObject);
    101.         }
    102.     }
    103. }
    Shooting

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Shooting : MonoBehaviour {
    5.    
    6.     public ParticleSystem muzzleFlash;
    7.     public GameObject impactPrefab;
    8.    
    9.     //Animator anim;
    10.     GameObject[] impacts;
    11.     int currentImpact = 0;
    12.     int maxImpacts = 5;
    13.     float damage = 25f;
    14.     bool shooting = false;
    15.    
    16.     void Start ()
    17.     {
    18.         impacts = new GameObject[maxImpacts];
    19.         for(int i = 0; i < maxImpacts; i++)
    20.             impacts[i] = (GameObject)Instantiate(impactPrefab);
    21.        
    22.         //anim = GetComponentInChildren<Animator> ();
    23.     }
    24.    
    25.     void Update () {
    26.        
    27.         if(Input.GetButtonDown ("Fire1"))
    28.         {
    29.             muzzleFlash.Play();
    30.             //anim.SetTrigger("Fire");
    31.             audio.Play ();
    32.             shooting = true;
    33.         }
    34.     }
    35.    
    36.     void FixedUpdate()
    37.     {
    38.         if(shooting)
    39.         {
    40.             shooting = false;
    41.            
    42.             RaycastHit hit;
    43.            
    44.             if(Physics.Raycast(transform.position, transform.forward, out hit, 50f))
    45.             {
    46.                 if(hit.transform.tag == "Player")
    47.                     hit.transform.GetComponent<PhotonView>().RPC ("GetShot", PhotonTargets.All, damage,
    48.                                                                   PhotonNetwork.player.name);
    49.                
    50.                 impacts[currentImpact].transform.position = hit.point;
    51.                 impacts[currentImpact].GetComponent<ParticleSystem>().Play();
    52.                
    53.                 if(++currentImpact >= maxImpacts)
    54.                     currentImpact = 0;
    55.             }
    56.         }
    57.     }
    58. }
     
  2. ronbonomo

    ronbonomo

    Joined:
    Oct 15, 2015
    Posts:
    32
    Sooooo sorry guys. Don't know why I typed it like this but in the shooting script, were you see audio.play, change it to GetComponent<AudioSource>().Play (); . That is what it is suppossed to be. Had a brain fart. Unity will fix it but good to let you know
     
  3. guitarxd

    guitarxd

    Joined:
    Nov 17, 2014
    Posts:
    3
    I'm wondering if you can help me out with the FPS Character Controller script. The free sample asset package has been updated since the tutorial came out and I'm not sure where to type the gun animation info since the script looks different.
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Is this the version done for Xmas 2014? Mike Geig did an update for Xmas 2015 that's in our live training archive right now.