Search Unity

Teleportation not working as intended

Discussion in 'Scripting' started by TheSnowyDev, Aug 6, 2020.

  1. TheSnowyDev

    TheSnowyDev

    Joined:
    Feb 17, 2020
    Posts:
    51
    So I'm working on an FPS game with a portal feature, and I can't really get the teleportation to work. I can instantiate the portals fine, but when I collide with the portal entrance to teleport, instead of teleporting to the portal exit point, the player is teleported to 0,0,0. Can someone please suggest a fix and/or tell me why this is occurring? There are no errors being thrown, just this problem.

    I am working in Unity 2018.4.25.

    Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PortalController : MonoBehaviour
    6. {
    7.     float range = 75f;
    8.  
    9.     int netCount;
    10.  
    11.     //The prefabs of the portals
    12.     public GameObject portalEnter;
    13.     public GameObject portalExit;
    14.  
    15.     //The clones of the portals so that the game doesn't delete the prefabs
    16.     GameObject portalEnterClone;
    17.     GameObject portalExitClone;
    18.  
    19.     [SerializeField] GameObject switcher;
    20.     public GameObject player;
    21.  
    22.     public Camera playerCam;
    23.  
    24.     public Transform portalEnterLocation;
    25.     public Transform playerTransform;
    26.  
    27.     Vector3 portalExitPos;
    28.  
    29.     void Start()
    30.     {
    31.         netCount = 0;
    32.     }
    33.  
    34.     void Update()
    35.     {
    36.         if (Input.GetButtonDown("Fire1") && netCount == 0)
    37.         {
    38.             SetNet();
    39.         }
    40.  
    41.         if(portalExitClone = null)
    42.         {
    43.             Destroy(portalEnterClone);
    44.         }
    45.     }
    46.  
    47.     void SetNet()
    48.     {
    49.         //Instantiates the first portal right next to the player. Followed the instructions, but is still trying to delete prefabs
    50.         portalEnterClone = Instantiate(portalEnter, portalEnterLocation.position, Quaternion.identity);
    51.  
    52.         //seting the second portal using raycasting.
    53.         RaycastHit hit;
    54.         if (Physics.Raycast(playerCam.transform.position, playerCam.transform.forward, out hit, range))
    55.         {
    56.             portalExitClone = Instantiate(portalExit, hit.transform.position, Quaternion.identity);
    57.             portalExitPos = portalExit.transform.position;
    58.             netCount++;
    59.             switcher.GetComponent<WeaponSwitcher>().SwitchToPistol();
    60.             switcher.GetComponent<WeaponSwitcher>().StartPortalCooldown();
    61.         }
    62.         else
    63.         {
    64.             //Instantiating the exit of the portal network, starting from the player's camera and following your code.
    65.             portalExitClone = Instantiate(portalExit, playerCam.transform.position + range * playerCam.transform.forward.normalized, Quaternion.identity);
    66.         }
    67.     }
    68.  
    69.     public void Teleported()
    70.     {
    71.         //Calling from PlayerController.cs after collision with portal entrance
    72.         playerTransform.position = portalExitPos;
    73.         Destroy(portalEnterClone);
    74.         Destroy(portalExitClone);
    75.  
    76.         netCount--;
    77.     }
    78. }
    79.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Use the clone's position not the prefab's position:
    Code (CSharp):
    1. portalExitPos = portalExitClone.transform.position;
     
  3. TheSnowyDev

    TheSnowyDev

    Joined:
    Feb 17, 2020
    Posts:
    51
    Still being sent to 0,0,0.
     
  4. TheSnowyDev

    TheSnowyDev

    Joined:
    Feb 17, 2020
    Posts:
    51
    @PraetorBlue it still is sending me to 0,0,0. What do you think is going on?
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Start adding Debug.Lig statements to your code. Print out the values of things to figure it out. Print out hit.point. print out portalExitClone's position. Etc...
     
  6. TheSnowyDev

    TheSnowyDev

    Joined:
    Feb 17, 2020
    Posts:
    51
    Thanks for the tip! I figured out that everything works fine when the ray hits something, but when there is nothing hit, the position of the portalExitClone is normal for a millisecond before it changes to 0,0,0. I would include a video, but it's too large, so here's some screenshots:



    2020.08.09-10.06.png 2020.08.09-10.26_01.png
     
  7. TheSnowyDev

    TheSnowyDev

    Joined:
    Feb 17, 2020
    Posts:
    51
    I HAD TO COPY-PASTE ONE SINGLE LINE ARE YOU KIDDING ME?
    Anyways, everything is solved now. Thanks for the help!