HSN720
HSN720

Reputation: 71

How to teleport player to different scene via trigger?

I have used the most traditional method of doing this but it doesn't work. No response is initiated.

I've attached a rigidbody2d collider (with 0.0001 mass and no drag or gravity) to my player sprite and a box collider 2d with is trigger checked.

Sidenote: Outdoor1" is the name of the scene I want to teleport my player to.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class TeleportToScene : MonoBehaviour
{
    [SerializeField] private string newScene;

    void OnTriggerStay2D(Collider2D other)
    {
        if (other.CompareTag ("Player"))
        {
                SceneManager.LoadScene("Outdoor1"); 

I expect it to go over the box collider and change scenes but nothing happens. No error messages either.

Upvotes: 0

Views: 928

Answers (1)

Horion Raphael
Horion Raphael

Reputation: 26

to make OnTriggerStay2D you need to have an collider 2D set to trigger on the object who as the script TeleportToScene and a Rigidbody2D

So you scene will be, 2 object:

1.Player with - Deplacement - Collider2D(not trigger) - Tag "Player"

2.Teleporter with - TeleportToScene.cs - Collider2D (trigger) - Rigidbody2D (kinematic)

Hope that help !

Upvotes: 1

Related Questions