teleport player using a interact script

so I made a interact script that when you press E will with in a box collider of the set object it triggers a script and teleports my player but nothing happens

Interact script :

    public float interactDistance = 5f;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            Ray ray = new Ray(transform.position, transform.forward);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, interactDistance))
            {
                if (hit.collider.CompareTag("portal"))
                {
                    hit.collider.transform.GetComponent<TeleScript>().UseTele();
                }
            }
        }     
    }

Teleport script:

public GameObject player;

    public void UseTele()
    {
        player.transform.position = new Vector3(5.08f, 1.01f, -19.65f);
        Debug.Log("TELEPORT");
    }

I dont think you need to use Raycasts…
you can use the “OnTriggerEnter” or “OnCollisionEnter” methods.

I tried and it work well.

  1. Make sure your player’s forward is towards the portal box. you can use Gizmos.DrawRay(transform.position, transform.forward * interactDistance); to see the ray in the scene view.
  2. Make sure the portal tag is assigned properly, it could be a mistake just like “Portal” instead of “portal”.