how do make a 2D player teleport to certain X and Y when they enter a trigger i tried flowing the unity tutorial but the way they showed was specific to there 2D game kit and i couldn’t put it in my game please help
Objects with a Collider component set as “trigger” will receive messages when other collider enter their collider.
So, you can create a game object with a Collider set as “trigger” and a script like this :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof (Collider2D))]
public class TeleportTrigger : MonoBehaviour
{
public enum TriggerType {Enter, Exit};
[Tooltip ("The Transform to teleport to")]
[SerializeField] Transform teleportTo;
[Tooltip ("The filter Tag")]
[SerializeField] string tag = "Player";
[Tooltip ("Trigger Event to Teleport")]
[SerializeField] TriggerType type;
void OnTriggerEnter2D (Collider2D other)
{
if (type != TriggerType.Enter)
return;
if (tag == string.Empty || other.CompareTag(tag))
other.transform.position = teleportTo.position;
}
void OnTriggerExit2D (Collider2D other)
{
if (type != TriggerType.Exit)
return;
if (tag == string.Empty || other.CompareTag(tag))
other.transform.position = teleportTo.position;
}
}
If you already know the position you want the player to move to, check if the player collided with the trigger and set it to the teleport position
[SerializeField] private Transform teleport;
private void OnTriggerEnter( Collider other )
{
if ( other.CompareTag( "Player" ) )
{
other.transform.position = teleport.position;
}
}
Assuming this is on the trigger game object…