Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

A problem with player teleportation.

A topic by Saurio created Jul 09, 2021 Views: 244 Replies: 3
Viewing posts 1 to 4

Hi. i want to teleport the player when it hits something but i don't know why it doesn't work.

this is my code:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class teleport : MonoBehaviour

{

    

    public GameObject spawner;

    public GameObject player;

    // Start is called before the first frame update

    void Start()

    {

       

        

    }

  

   

    private void OnTriggerEnter(Collider other)

    {

        if (other.CompareTag("Player"))

        {

            player.transform.position = new Vector3(spawner.transform.position.x, spawner.transform.position.y, spawner.transform.position.z);

        }

    }

}

If anyone knows why I would appreciate if you could tell me.

(+1)

Sorry, not a Unity dev, but did you try adding logging to make sure the OnTriggerEnter event was getting called? Also, is there possibly other logic that is updating the player’s position AFTER the event runs (thus canceling out the teleport)?

(1 edit) (+1)

Make sure both objects have a box colider if its a 3D game (if its a 2D game use a 2D Box colider and make sure both objects are on the same Z Position) double check if the tag is actually on the player and is actually called "Player" as tags need to be exact. btw if the game is 2D you would use OnTriggerEnter2D.

Let me know if this helps

thanks for the support. What I did at the end was reload the scene so that it appeared from the beginning, and although it is not the same, it is used for what I want to do. thanks again for the answers.