Dec
28
2013

TerraVol + Photon Networking

terravol_photon_multiplayer_1

Had to test it, how it would work if connect TerraVol with Photon.. Surprisingly it only took few clicks!

Webplayer demo:
coming later

Notes

–    Import/Open TerraVol project (needs to be purchased from assetstore)
–    Import Photon networking http://u3d.as/content/exit-games/photon-unity-networking-free/2ey
–    Setup photon (follow instructions, mainly just enter your appId and press save..)
–    Open scene “DemoWorker-Scene” (it has lobby system)
–    From build settings remove everything
–    Add that “DemoWorker-Scene” scene to build settings as scene 0
–    Open scene “random_generate_scene”
–    Save as new scene “scene_multiplayer” and add it to build settings as scene 1
–    In the scene “DemoWorker-Scene” select Scripts object from Hierarchy
–    Open Worker Menu script
–    Change line:

public static readonly string SceneNameGame = "DemoWorkerGame-Scene";

–    into:

public static readonly string SceneNameGame = "scene_multiplayer";

–    Quick test: Press Play (lobby should appear, press Create, Terravol scene should be loaded”
–    Open “DemoWorkerGame-Scene”
–    Copy “Main Camera”, “GuiText: Demo Worker”, “Scripts” from Hierarchy
–    Open “scene_multiplayer” and paste those objects there
–    Disable old “Main camera” (it has the character controller and stuff)
–    Select scripts object, rename it as “PhotonScripts”
–    Set “PhotonScripts “ Y position to 120 (player is instantiated at this position when connected)
–    In build settings, switch to Webplayer platform
–    Player settings, enable: [x] Run in background
–    Build & Run
–    Webplayer starts, Lobby: Create Room
–    Press play in editor, Lobby: Join room
–    Yay! 2 players in terravol scene..but the terrains are randomly generated..
–    Not sure if there is some fixed seed option in TerraVol yet.. so:
–    Open “TerraMap” script
–    Find:

tmg.randomize = true;

–    Replace with:

tmg.randomize = false;

–    Seems to work, same level on both.
–    From the old Main Camera, copy Builder script component to the new Main Camera
–    Can dig the terrain (although it happens under the player?) and its not synced..
–    Add PhotonView component to MainCamera (View id seems to be 2)
–    Open the “Builder” script
i-have-no-idea-what-im-doing-dog
–    Find line:

DoAction( hit );

–    Replace with:

if (hit!=null)
{
PhotonView photonView = PhotonView.Get(this);
photonView.RPC("DoAction", PhotonTargets.All, hit.Value.x,hit.Value.y,hit.Value.z);
}

–    Find line:

public void DoAction (Vector3? point)

–    Replace with:

[RPC]
public void DoAction (float hx, float hy, float hz)
{
Vector3? point = new Vector3(hx,hy,hz);

– Had to do that with 3 floats, Vector3 or Vector3? was giving some errors when used as rpc parameter.
–    What is that “?” http://stackoverflow.com/questions/2326158/why-is-there-a-questionmark-on-the-private-variable-definition
–    Check also Photon RPC: http://doc.exitgames.com/en/photon-realtime/PhotonUnityNetworkingIntro#_rpcpun
–    Build, Run, Create..
–    Editor, Hit Play, Join, Modify terrain!
–    It works!!1

TODO:
–    Use original FPS/Character controller
–    Then that should fix the mousehit point also (currently digging happens around the player..)
–    What happens when another player is in a different chunk..
–    Explosions, Cannons, Meteorites..or not.

*******************************************************

Part2 (use terravol original character controller)

–          Rename the new Main Camera as “Main Camera Photon”
–          Then disable it
–          Rename the old Main Camera as “Main Camera TerraVol”
–          Then enable it
–          Drag & Drop Photon View component from the “Main Camera Photon” into “Main Camera TerraVol” (You could just remove it from the old and add to the new manually also)
–          Create new folder in Project window “Resources”
–          Create prefab from “Main Camera TerraVol” by dragging it from Hierarchy window into that “Resources” folder in Project window
–          In “PhotonScripts” gameobject, assign that  new prefab into Player Prefab field
–          In Map gameobject, assign that  new prefab into Player Camera  field (*this could be bad, since its referencing the prefab..not scene object?)
–          Build & Run, test in editor, Seems to work! Just sometimes its digging in wrong direction?

 


8 Comments + Add Comment

  • Thank you so much! I just stumbled across this and it’s something I really needed!

    (I love Terra Vol and I’ve been meaning to photon network the voxels, now I have no more excuses!)

    Could we also have a tutorial on using the new A* feature included with TerraVol to make units pathfind across the voxel terrain?

  • Any update to this? like generating new chunks instead of the default 4×4

    • no updates on this.. but I guess new chunks should be matching on all clients, since its fixed random, maybe would need to create new chunks with rpc call, if all players need to see it at the same time..(especially if other player goes digging there), but havent tried.

  • Assets/TerraVolPack/TerraVol/Scripts_to_customize/Builder.cs(265,15): error CS1525: Unexpected symbol `private’

    when i try and do the editing terrain part of the tutorial i get this error

    • ok fixed the last one but now i get this error and it dosent allow me to build

      NullReferenceException: Object reference not set to an instance of an object
      Builder.Update () (at Assets/TerraVolPack/TerraVol/Scripts_to_customize/Builder.cs:94)

  • how do i get terrain to generate around the second player connecting?

    • hey Greg.

      Have you found a way to generate the map aournd the second player?
      For me it worked, but not that good, it’s all messed up.
      As you can only assign 1 camera to the generator and it so only 1 player can expand the map.
      If you try to switch between camera’s, the map is all messy.

      you found a way?

  • Old old post. But pertains to what I’m doing. Incase anyone comes across it as I did.

    I think you would have to have each player who is spawning send that to the master, and have them tell everyone else. I don’t use TerraVol but the Voxel system I use is the method I’m using. The problem is new connecting players. The best I can do there is buffer but eventually there will be a ton of RPCs buffered. Doesn’t seem right.

    There needs to be a way to break down the chunk data and synch it over the stream. (For me anyway, using a heightmap to dictate a finite world)

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Github Join Discord Twitch Instagram BlueSky

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.