Hello Guest

Author Topic: Request help with tk2dCamera and some concepts.  (Read 4401 times)

mrpacogp

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Request help with tk2dCamera and some concepts.
« on: April 14, 2014, 12:38:07 pm »
Hi there.
I have read the documentation looking for examples for my problem.
I have made Jump2Sky without problems with tk2dCamera :
https://play.google.com/store/apps/details?id=com.jump2sky.com

My camera is a free object on hierarchy and follow the player with one script.

Now i`m working on multiplayer mode with 2-4 players and want to now how can i do it for get the camera followin for separate each one.

I have found on this forum to use Rect    ScreenExtents [get] but i cant figure how.

When the multiplayer game starts, the multiplayer game instantiate one player prefab, but the maincamera tk2d its on instacen
I have to put the tk2d main on each player? I have to use screenextents ?

Neeko

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 59
    • View Profile
    • Overdeveloped
Re: Request help with tk2dCamera and some concepts.
« Reply #1 on: April 14, 2014, 02:58:29 pm »
So you want to do split-screen, where each player's camera takes up it's own portion of the screen? If so, you simply create a new tk2dCamera that'll follow each player, and adjust the tk2dCamera.CameraSettings.rect of each to change the viewport size. So if you wanted to have four separate cameras for four players, each having a corner of the screen, you would do something like

Code: [Select]
// Setup 4 camera viewports, one in each corner of the screen.
List<Rect> splitViewportRects = new List<Rect>();
splitViewportRects.Add(new Rect(0, 0.5f, 0.5f, 1));
splitViewportRects.Add(new Rect(0.5f, 0.5f, 0.5f, 1));
splitViewportRects.Add(new Rect(0, 0, 0.5f, 0.5f));
splitViewportRects.Add(new Rect(0.5f, 0, 0.5f, 0.5f));
// Assuming you have reference to all players in a players array.
for (int i = 0; i < players.Count; ++i) {
    // Assuming you have a prefab representing your tk2dCamera in the Resources/Prefabs folder.
    GameObject camera = Instantiate(Resources.Load("Prefabs/tk2dCamera")) as GameObject;
    tk2dCamera cameraScript = camera.GetComponent<tk2dCamera>();
    // Give each player a corner viewport on the screen.
   cameraScript.CameraSettings.rect = splitViewportRects[i];
   // And then set camera target to indexed player
   // Assuming you have some type of camera controller script on your camera prefab
   CameraController cameraController = camera.GetComponent<CameraController>();
   cameraController.Target = players[i];
}

If not, a single camera can only ever follow a single target. You could calculate a position that's the relative center of four different targets, and have the camera follow that. I wrote a blog post awhile back explaining that kind of system in further detail here http://overdevelop.blogspot.com/2014/01/single-camera-system-for-four-players.html
« Last Edit: April 14, 2014, 03:19:46 pm by Neeko »

mrpacogp

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Request help with tk2dCamera and some concepts.
« Reply #2 on: April 14, 2014, 05:23:28 pm »
Thank you Neeko for your support.

Apologize about my english.

I`m working on android device.

I want the camera following each player on their device.

If you download my game, you can test mode solo, and watch how the camera follow my player.
But when i start a multiplayer mode, i need to instantiate each playerprefab on my scene, but i only have one main camera, its the problem.
I have to put the main camera on playerprefab?
« Last Edit: April 14, 2014, 05:43:42 pm by mrpacogp »

Neeko

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 59
    • View Profile
    • Overdeveloped
Re: Request help with tk2dCamera and some concepts.
« Reply #3 on: April 14, 2014, 07:19:32 pm »
This seems like a networking related question then, outside the scope of 2D Toolkit so you may have better luck on the Unity forums with this (anyone please correct me if I am wrong).

I don't have too much experience with networking in Unity, but I imagine that each client would just have a single camera instance for their own player object and you would sync the positions of the players across the clients. https://docs.unity3d.com/Documentation/Components/NetworkReferenceGuide.html is a good place to start researching about networking.