Search Unity

Seamless Teleporting

Discussion in 'Scripting' started by tobad, Mar 26, 2015.

  1. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    Hello Guys,

    I'm looking for a good solution for my seamless teleport.
    V3 is actually the only one that is looking seemless with collision detection on both sides. But its completly inefficient (1 Character = 3 Character).

    This is just an example, CharacterGameObjects from my Game have alot of Childs full of Scripts and Colliders and Triggers. Its also an Multiplayergame for Mobile devices so 4 Player with Teleport v3 will result in (4*3=) 12 Character calculations.

    Is there another way of implementing a seamless teleporting?

    Teleport V1 (OnTrigger)

    Code (CSharp):
    1. public class TeleportScript : MonoBehaviour {
    2.    
    3.     void OnTriggerEnter2D(Collider2D other)
    4.     {
    5.         Debug.Log("OnTriggerEnter2D " + other.tag );
    6.         if(other.tag == "Player")
    7.         {
    8.             other.transform.position = new Vector3(other.transform.position.x*-1, other.transform.position.y, other.transform.position.z);
    9.         }
    10.     }
    11.  
    12. }

    Teleport V2 (transform.position condition)

    Code (CSharp):
    1.         public float rightEdge = 12.5f;
    2.         public float leftEdge = -12.5f;
    3.  
    4.         public Vector2 TeleportOffset = new Vector2(0.5f,2f);
    5.  
    6.         private void Teleport()
    7.         {
    8.             if(this.transform.position.x > rightEdge)
    9.             {
    10.                 this.transform.position = new Vector3(leftEdge + TeleportOffset.x, transform.position.y, transform.position.z);
    11.             }
    12.             else if(this.transform.position.x < leftEdge)
    13.             {
    14.                 this.transform.position = new Vector3(rightEdge - TeleportOffset.x, transform.position.y, transform.position.z);
    15.             }
    16.         }
    Teleport V3 (CharacterGO has to Clones as Child left and right)

    PlayerCharacter has two Childs with all the same components and child transforms. The Position of the Clone childs is the same as the PlayerCharacter except the x value +/- Positionoffset (in this example transform.position.x = +/- 25)


    Code (CSharp):
    1.         public float rightEdge = 12.5f;
    2.         public float leftEdge = -12.5f;
    3.  
    4.         public Vector3 TeleportVector = new Vector3(25f,0f,0f);
    5.  
    6.         private void TeleportV3()
    7.         {
    8.             // check if this is the root character
    9.             if(this.name == "Left")
    10.             {
    11.                 // child clones do not teleport!
    12.                 this.transform.position = this.transform.parent.position - TeleportVector;
    13.                 return;
    14.             }
    15.             else if(this.name == "Right")
    16.             {
    17.                 // child clones do not teleport!
    18.                 this.transform.position = this.transform.parent.position + TeleportVector;
    19.                 return;
    20.             }
    21.  
    22.             if(this.transform.position.x > rightEdge)
    23.             {
    24.                 this.transform.position = new Vector3(leftEdge, transform.position.y, transform.position.z);
    25.             }
    26.             else if(this.transform.position.x < leftEdge)
    27.             {
    28.                 this.transform.position = new Vector3(rightEdge, transform.position.y, transform.position.z);
    29.             }
    30.         }
     

    Attached Files:

  2. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    What ...

    So what does seamless mean? It looks like you're just moving the object from position A to position B in one frame update. If you have objects that need to move with the main character just parent them to the one object, move the object, then next frame un-parent them. They'll move right along with the parent. I'll be honest though. I'm not really grasping what you're trying to do...
     
  3. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    in v3 i'm already parenting them and because collision should work on left and right side during the transition the CharacterGameObject needs those childs all the time.

    - players should always be visible (can't run off camera)
    - if player is moving left out of the camera boundary (is only partly visible) the other party of his sprites should be visible at the right edge of the camera boundary

    Edit: Valves Portal has the same mechanic (Pictures)

    sorry, can't describe it better in english.
     

    Attached Files:

    Last edited: Mar 26, 2015
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can't do it without the dummy models. Note the dummy does not need to be a deep clone of the original. It just needs to pass on the details from any collision.
     
  5. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    i modified my character
    every collider/trigger exists now 3 times in the character itself

    for example child transform Head has one ReceiveDamageScript and 3 BoxCollider2D's
    • one has -xOffset
    • one has no xOffset
    • one has +xOffset
    with this design i dont need 3 ReceiveDamageScripts (one for each Character Clone)!
    my dummy's (clones) only need:
    + SpriteRenderer
    + SetParentSpriteScript

    is there a more efficient method?
     
  6. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    @BoredMormon

    is there a performance difference between design A and B? What would you personally prefer?

    A) 1 GameObject with 3 BoxCollider2D and 1 TriggerScript
    B) 3 GameObjects with one BoxCollider2D and one TriggerScript

    I need to tweak this setup up, at the mement i can only seamless teleport in horizontal and i need vertical teleporting too. so i want to optimize it.

    Aa) 1 GameObject with 5 BoxCollider2D and 1 TriggerScript for each body part
    Bb) 5 GameObjects with one BoxCollider2D and one TriggerScript for each body part

    1x Character (4 Body Parts) would result in

    AaA) 4x 1 GameObject with 5 BoxCollider2D and 1 TriggerScript
    BbB) 4x 5 GameObjects's with one BoxCollider2D and one TriggerScript
     

    Attached Files:

    Last edited: Mar 30, 2015