Author Topic: Updating a players position OnNetworkPlayerJoin  (Read 2709 times)

r6834

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Updating a players position OnNetworkPlayerJoin
« on: April 05, 2014, 02:58:50 AM »
Hi, I am new to TNet and I am trying to figure out some of the basics. So far it has been a great asset for a beginner. Following some of the examples, I was trying to update a previous players positions during an "OnNetworkPlayerJoin" function call. I am not getting anything at all. Can anyone show me where I may be overlooking things? Thanks for the help.

  1. #pragma strict
  2. private var agent : NavMeshAgent;
  3. var tno : TNObject;
  4.  
  5.  
  6. function Start () {
  7.  
  8.         agent = GetComponent.<NavMeshAgent>();
  9.         tno = GetComponent.<TNObject>();
  10.  
  11. }
  12.  
  13. function Update () {
  14.  
  15.         if (tno.isMine) {
  16.                 var hit: RaycastHit;
  17.                 if (Input.GetMouseButtonDown(0)) {
  18.                
  19.                         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  20.                                
  21.                         if (Physics.Raycast(ray, hit)) {
  22.                                 tno.Send("SetTarget", TNet.Target.All, hit.point);
  23.                         }
  24.                 }
  25.         }
  26.  
  27. }
  28.  
  29. function OnNetworkPlayerJoin() {
  30.  
  31.         tno.Send("SetTargetImmediate", TNManager.player, transform.position);
  32.  
  33. }
  34.  
  35. @TNet.RFC
  36. function SetTarget (tar) {
  37.  
  38.         agent.SetDestination(tar);
  39.  
  40. }
  41.  
  42. @TNet.RFC
  43. function SetTargetImmediate (pos : Vector3) {
  44.  
  45.         transform.position = pos;
  46.  
  47. }
  48.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Updating a players position OnNetworkPlayerJoin
« Reply #1 on: April 05, 2014, 12:04:24 PM »
OnNetworkPlayerJoin is a notification of joining a channel. It has a parameter you're missing -- parameter that tells you which player has joined.

There is no need to send anything inside that function. In fact, doing so is wrong because that function will be called on all clients, resulting in all clients trying to send the position immediately. You're also send that RFC message to yourself. Not sure why.

Instead simply send the player position using Target.AllSaved instead of Target.All, and it will be set for everyone even before OnNetworkPlayerJoin is called.

r6834

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Updating a players position OnNetworkPlayerJoin
« Reply #2 on: April 05, 2014, 02:31:23 PM »
OnNetworkPlayerJoin is a notification of joining a channel. It has a parameter you're missing -- parameter that tells you which player has joined.

There is no need to send anything inside that function. In fact, doing so is wrong because that function will be called on all clients, resulting in all clients trying to send the position immediately. You're also send that RFC message to yourself. Not sure why.

Instead simply send the player position using Target.AllSaved instead of Target.All, and it will be set for everyone even before OnNetworkPlayerJoin is called.

Thanks Aren! The issue I am running into doing it this way is as follows :

"Exception has been thrown by the target of an invocation. Player.SetTarget (UnityEngine.Vector3)"

Here is what the code looks like now :

  1. function Update () {
  2.  
  3.         if (tno.isMine) {
  4.                 var hit: RaycastHit;
  5.                
  6.                 if (Input.GetMouseButtonDown(0)) {
  7.                
  8.                         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  9.                                
  10.                         if (Physics.Raycast(ray, hit)) {
  11.                                 agent.SetDestination(hit.point);
  12.                                 tno.Send("SetTarget", TNet.Target.OthersSaved, hit.point);
  13.                         }
  14.                 }
  15.                
  16.         }
  17.  
  18. }
  19.  
  20. @TNet.RFC
  21. function SetTarget (tar) {
  22.  
  23.         agent.SetDestination(tar);
  24.  
  25. }
  26.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Updating a players position OnNetworkPlayerJoin
« Reply #3 on: April 06, 2014, 03:05:52 AM »
That tells you that your function threw a null exception. My guess is "agent" is null.

r6834

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Updating a players position OnNetworkPlayerJoin
« Reply #4 on: April 06, 2014, 09:39:02 PM »
What's strange is that it is still working despite calling the exception. However, what is not working is my "SetTargetImmediate" function. Is using the NavMeshAgent not a good approach?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Updating a players position OnNetworkPlayerJoin
« Reply #5 on: April 08, 2014, 01:44:13 AM »
I can't say as I've never tried using NavMeshes. I'd recommend adding some null checks just to be safe.