Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Third Party Unity multiplayer through Mirror - teleporting player inconsistent

Discussion in 'Multiplayer' started by dddoc, Apr 13, 2020.

  1. dddoc

    dddoc

    Joined:
    Feb 13, 2020
    Posts:
    1
    Hello,
    I am working at a simple co-op game at the moment but I am having a problem with respawning players after death.
    What I wanted to acomplish is that when a player dies, I want to switch the camera to a different one for 10 seconds and then teleport the player to a spawn location. Also I need to 'hide' the player so others can't see him while he's dead.
    My solution was to teleport him under the map when he dies so he's hidden and when it's time to spawn, teleport him back.

    Code (CSharp):
    1. internal override void Die()
    2.     {
    3.         //Disable player Input
    4.         gameObject.GetComponent<PlayerMovement>().enabled = false;
    5.         gameObject.GetComponent<PlayerCombat>().enabled = false;
    6.  
    7.         //Teleport the dead player under the map
    8.         gameObject.GetComponent<PlayerMovement>().ResetPosition(new Vector3(0, -15, 0));
    9.  
    10.         //Swap Cameras, disable crosshair, enable countdown
    11.         _playerCamera.enabled = false;
    12.         _deathCamera.enabled = true;
    13.         _crosshair.enabled = false;
    14.         _respawnTimer.enabled = true;
    15.  
    16.         //Timer untill respawn
    17.         var Timer = _respawnTimer.GetComponent<RespawnTimer>();
    18.         Timer.Timer = 10f;
    19.  
    20.         //Spawn after 10 seconds
    21.         Invoke("Spawn", 10);
    22.     }
    23.  
    24.     private void Spawn()
    25.     {
    26.         //Set HP to max
    27.         HitPoints = MaxHp;
    28.  
    29.         //Swap Cameras, enable crosshair, disable countdown
    30.         _respawnTimer.enabled = false;
    31.         _deathCamera.enabled = false;
    32.         _crosshair.enabled = true;
    33.         _playerCamera.enabled = true;
    34.  
    35.         gameObject.GetComponent<PlayerMovement>().ResetPosition(new Vector3(-12, 6, -11));
    36.  
    37.         //Enable Player Input
    38.         gameObject.GetComponent<PlayerMovement>().enabled = true;
    39.         gameObject.GetComponent<PlayerCombat>().enabled = true;
    40.     }
    PlayerMovement.ResetPosition takes a vector and teleports the player to that place as well as reseting his movement speed.

    Code (CSharp):
    1.     public void ResetPosition(Vector3 position)
    2.     {
    3.         transform.position = position;
    4.         CurrentSpeed = Vector3.zero;
    5.     }
    The problem is that it is very inconsistent and sometimes he stays under the map where I teleported him.
    My guess is that it is a problem with NetworkTransform component and its sync, but I am not sure at all.

    upload_2020-4-13_21-29-3.png

    Anyone any ideas?
    Thanks!
     
  2. erenbaltali16

    erenbaltali16

    Joined:
    Apr 13, 2020
    Posts:
    2
    I have the same problem.My players are incostintent while i'm teleporting them to another place.Did you you find a way to solve it?
     
  3. unity_87d2-qhplC8dQQ

    unity_87d2-qhplC8dQQ

    Joined:
    Jul 25, 2020
    Posts:
    1
    Any chance youve solved this?
     
  4. Dominik-L

    Dominik-L

    Joined:
    Apr 12, 2017
    Posts:
    2
    I am not sure if our problem is related to this one, but...
    We had a CharacterController on every player enabled and this caused an inconsistent teleporting.
    We switched to Mirror's ServerTransform (instead of just using transform.position) and now disable the CharacterController before every teleporting and enable it after the player has been teleported.
     
  5. bolska

    bolska

    Joined:
    Jul 19, 2018
    Posts:
    8
    Disabling the CharacterController before teleporting solved my problem. Thanks.
     
  6. JikaiNoTsubasa

    JikaiNoTsubasa

    Joined:
    Apr 28, 2016
    Posts:
    2
    Hi, I'm still having this issue even if the character controller is disabled before teleporting.
    I do it from the client as it has Authority on transform.
    Code (CSharp):
    1. Vector3 pos = sp.GetSpawnPosition();
    2. controller.enabled = false;
    3. transform.position = pos;
    4. controller.enabled = true;
    But still doesn't work every time, sometimes yes and sometimes no...
     
  7. Dominik-L

    Dominik-L

    Joined:
    Apr 12, 2017
    Posts:
    2
    That might not work, because it takes some time to teleport the player.
    We added a Coroutine that enables the controller with a 500ms delay after the teleporting - maybe not the most elegant way, but it worked :D
     
  8. galnikkozmelj

    galnikkozmelj

    Joined:
    May 29, 2019
    Posts:
    7
    disabling and enabling controller between teleportation worked for me too!
     
  9. Kayckbr

    Kayckbr

    Joined:
    Oct 25, 2021
    Posts:
    5
    i can confirm that disabling component right before teleport fixes it (i was having this issue in singleplayer)! thanks for this thread, i just have disabled the component right before the teleport happen then enable right after it teleports.
     
  10. Iron_Bee

    Iron_Bee

    Joined:
    Dec 9, 2023
    Posts:
    1
    I solved this problem by just disable Character Controller and when player is teleported enable