Author Topic: TNManager.Instantiate position issues  (Read 8102 times)

rxmarcus

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 62
    • View Profile
TNManager.Instantiate position issues
« on: June 29, 2017, 07:24:17 PM »
I'm hoping I can get some advice on what I might be doing wrong here.

Basically I'm spawning a grenade object like so

  1. TNManager.Instantiate("GrenadeCreated", "GrenadeProjectile", false, playerNumber, playerCharacterController.weaponBone.transform.position, Quaternion.Euler(0, 0, aimAngle), grenadeVector);
  2.  
  3. [RCC]
  4.     static GameObject GrenadeCreated(GameObject prefab, int playerNumber, Vector3 pos, Quaternion rot, Vector2 velocity)
  5.     {
  6.         Debug.Log("Spawning grenade at: " + pos);
  7.         // Instantiate the prefab
  8.         GameObject go = prefab.Instantiate();
  9.         go.GetComponent<Script_GrenadeProjectile>().ProjectilePNum = playerNumber;
  10.         go.transform.position = pos;
  11.         go.transform.rotation = rot;
  12.         go.GetComponent<Rigidbody2D>().velocity = velocity;
  13.  
  14.         return go;
  15.     }
  16.  

The issue I'm having is that on the other clients (not the owner of the tno) the grenade seems to spawn at 0,0,0. As you can see in the provided screenshot. (I have a trail renderer on the grenade so you can see it spawns at 0,0,0 and quickly whips over to the positions that the owner are updating to sync the transforms position.



Any thoughts why my grenades are spawning at 0,0,0 rather than what I've set in my [RCC] method?

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: TNManager.Instantiate position issues
« Reply #1 on: July 01, 2017, 06:05:08 AM »
What's your Debug.Log say? What is it expected to say?

PS: did you solve the TNManager.LoadLevel problem from your other thread?

rxmarcus

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 62
    • View Profile
Re: TNManager.Instantiate position issues
« Reply #2 on: July 03, 2017, 12:44:33 PM »
It gives (0,0) for the client and it should give the position of the players gun something like (5, 8) in the game world. (Which it does for the Host).

I didn't fully resolve my LoadLevel issue yet. It seems to be working for me to manually call tno.DestroySelf() on my player network objects before I call LoadLevel so I left off there. However I'd like to get it working the correct way as I'm sure TNet can save me headaches in regards to cleaning up properly.

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: TNManager.Instantiate position issues
« Reply #3 on: July 03, 2017, 02:37:50 PM »
Try placing a Debug.Log just before the Instantiate call too. I just tested this using your code and it works as expected.

Is it possible that you're getting a null reference exception here: go.GetComponent<Script_GrenadeProjectile>().ProjectilePNum?
Execution would stop there, so the position wouldn't be set. It's a prefab though so no reason why it'd be different on host vs non-host.
I suppose it's also possible that playerCharacterController.weaponBone.transform.position is a Vector2 but your method takes a Vector3 so maybe TNet gets confused? That's a stretch, but something to check.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNManager.Instantiate position issues
« Reply #4 on: July 06, 2017, 06:23:01 AM »
When you instantiate an object, it will always be at 0 0 0 first until the position is updated. Certain Unity components start all their logic in Awake or OnEnable (like the line renderer, I'm guessing?). Awake and OnEnable are called as soon as you call Instantiate(), and before you get a chance to set the transform's position and rotation.

If you don't want this behaviour, pass "null" for the prefab name. This will result in an empty game object being passed to the RCC function which you can then position first, then use AddComponent to add the elements you need, effectively building it dynamically. You can also instantiate a child object here, creating it from a pooled object list containing disabled game objects, set its position and only then enable it. This would bypass the Awake/OnEnable limitation as the object will be enabled only after the position has been set.

For objects that will only exist for a short time and travel with an expected path that doesn't need to be synchronized past the initial initialization (such as bullets or grenades), I strongly advise you to use RFCs instead of RCC to spawn them. RCCs require an object ID to be created and the object to get added to a list of persistent objects. RFCs don't. You can call a one-time (non-saved) RFC to spawn your projectile instead of calling the Instantiate method.