Author Topic: First person controller not working as expected  (Read 11104 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: First person controller not working as expected
« Reply #15 on: May 13, 2014, 09:27:52 AM »
TNManager.isThisMyObject should be used in Awake(), while tno.isMine should be used everywhere else.

TNet's RFCs are sent to TNObject. TNObjects are identified by IDs. If two TNObjects share the same ID, then they will both receive functions, and it may not be clear who should actually be receiving things. It's important to give TNObjects unique IDs.

If I understand your example correctly, you seem to have the join script and the spawn script in the same scene? This is not quite correct. Join should be in one scene, which will then load another scene -- and the player instantiation should happen in this second scene.

djray2k

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 4
  • Posts: 74
  • Blockey Hockey!
    • View Profile
    • Walking Talking Grilling Bear Studios
Re: First person controller not working as expected
« Reply #16 on: May 13, 2014, 10:17:22 AM »
Ah sorry, I didn't realize you were disabling things.

So what you currently have enabled related to TNet:
  • a Connection Object with a TNManager script (with a Player prefab) and a Connection Controller script

... and disabled:
  • PlayerSpawn Object with TNAutoCreate script (without a prefab)
  • TNJoin Object with TNManager (leftover presumably) and a TNAutoJoin script that has Game has the first level

With just the enabled objects, all you have is a TNManager and your connection controller script:

  1.        
  2. // Use this for initialization
  3. void Start () {
  4.         TNManager.Connect( "aursand.no:5127" );
  5. }
  6.  
  7. void OnNetworkConnect ( bool success, string message ) {
  8.         if ( success ) {
  9.                 TNManager.JoinChannel( 1, "Game" );
  10.         }
  11. }
  12.  
  13. void OnNetworkJoinChannel ( bool success, string message ) {
  14.         if ( success ) {
  15.                 TNManager.Create( "Prefabs/Player", false );
  16.         }
  17. }
  18.  



One problem I see is that the first level you're trying to join is the same scene all of this is within. Your first level that you reference is called "Game" which is the name of the scene.

You should create a connection scene that goes to the Game scene on connection. That scene would have the Connection Object with a TNManager script. Your TNJoin Object should also be present, and assuming all the connection details like address and port are correct you shouldn't have to change anything.

I haven't checked out the latest update which says the TNManager doesn't need to be present all the time, but I would make sure your Connection object with the TNManager carries over to the new scene.

In your game scene, you should have your PlayerSpawn object with a player prefab (same one you put in TNManager).


tl;dr You're trying to go to the same scene you're in when you connect to the server.

toreau

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: First person controller not working as expected
« Reply #17 on: May 13, 2014, 11:15:19 AM »
Before I redo all this, I need to ask: does TNet require your game to have more than one scene? If so, why? That's a dependency that should be documented, no?

djray2k

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 4
  • Posts: 74
  • Blockey Hockey!
    • View Profile
    • Walking Talking Grilling Bear Studios
Re: First person controller not working as expected
« Reply #18 on: May 13, 2014, 01:09:56 PM »
I can't say for sure, but I think the general Idea is that joining a game session should be the deciding factor on whether you enter a game scene. Normally you'll have a menu scene that you will start the game from that allows you to enter an online session.

Early on, I'd just create a temporary scene since you goal is to use the autojoin script. In the videos he does this with a simple gui that says "Connecting". It enters the game if successful, and enter the disconnected scene if it doesn't

toreau

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: First person controller not working as expected
« Reply #19 on: May 13, 2014, 01:34:00 PM »
Ok. I'm fine with that requirement as long as I'm aware of it. :)

Now!

I created a brand new project while following this tutorial, and while I change some object names here and there to be a bit more "non-tutorial", AND add some FPS-stuff to everything, it still doesn't work as expected; the second player joining the game is controlling the first player joining the game.

Download the whole thing here if you want to try it out. What I do, is:

  • Build the game, run around a bit to make sure everything works as expected.
  • Play the game from inside Unity3D, run around a bit to make sure everything works as expected.
  • Switch back to the build game, while making sure I can watch the other player, move around, and I can see that my "build movements" are affecting the other player.

I can't for the love of (put in anything here) understand what I'm doing wrong?!
« Last Edit: May 13, 2014, 01:51:19 PM by toreau »

djray2k

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 4
  • Posts: 74
  • Blockey Hockey!
    • View Profile
    • Walking Talking Grilling Bear Studios
Re: First person controller not working as expected
« Reply #20 on: May 13, 2014, 02:55:30 PM »
Just checking it real quick, I see that you're creating a camera for each player. You should disable the cameras that aren't related to the main player. I can actually tell that that the players are moving and rotating normally, just the camera is wrong.

toreau

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: First person controller not working as expected
« Reply #21 on: May 13, 2014, 04:15:49 PM »
Huh? The camera is part of the Player prefab being instantiated...?

djray2k

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 4
  • Posts: 74
  • Blockey Hockey!
    • View Profile
    • Walking Talking Grilling Bear Studios
Re: First person controller not working as expected
« Reply #22 on: May 13, 2014, 04:24:09 PM »
Right, the thing is you have multiple active cameras that draw the whole screen. You have n cameras for n amount of people who join.

You'll need to have something in a script within your Player prefab that disables the camera for the remote Players created.

Don't change the prefab, the prefab is fine.

toreau

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: First person controller not working as expected
« Reply #23 on: May 13, 2014, 04:47:49 PM »
That easy! :)

The solution was to disable all of the "player-related" components, and then (inside my PlayerController.cs) enable them if you are "the player":

  1.         void Start () {
  2.                 if ( tno.isMine ) {
  3.                         characterController = GetComponent<CharacterController>();
  4.                         playerCamera        = characterController.GetComponentInChildren<Camera>();
  5.  
  6.                         characterController.enabled = true;
  7.                         playerCamera.enabled = true;
  8.                 }
  9.         }
  10.  

Thanks!

toreau

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: First person controller not working as expected
« Reply #24 on: May 14, 2014, 11:26:35 AM »
I've got things working as I want now, but I have more complaints. :D

I currently work with the three scenes - Connect, Game and Disconnect - but I find it extremely annoying to spend 99% of my work in the Game scene, and having to load the Connect scene every time I want to play the game.

Is this really necessary, or are there ways around it?
« Last Edit: May 14, 2014, 11:53:53 AM by toreau »

djray2k

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 4
  • Posts: 74
  • Blockey Hockey!
    • View Profile
    • Walking Talking Grilling Bear Studios
Re: First person controller not working as expected
« Reply #25 on: May 14, 2014, 12:08:46 PM »
You could add a script that checks if it is connected while within the game scene. If it is you can load the connection scene. I actually need to do something like this too.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: First person controller not working as expected
« Reply #26 on: May 15, 2014, 12:30:28 AM »
In a normal game you will have multiple scenes anyway. Menu scene, game scene. Disconnect scene is completely optional -- you can simply show a UI dialog in the game scene.

The menu scene -> game scene transition is a good idea because you want the game scene to be a "clean slate" for everyone who joins -- and it's a lot more difficult to do that with an existing scene.

Remember -- you only need to go through the menu scene if you're actually testing multiplayer. If you start with the game scene, TNet will work exactly the same as if you were connected and happen to be the only player there. All the RFCs will still work, and TNManager.isHosting / tno.isMine will return expected values as well. Makes it easy to test.