The question is answered, right answer was accepted
how to make a 2D player teleport to certain X and Y when they enter a trigger
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
Answer by UnityCoach · Mar 13, 2018 at 02:43 PM
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;
}
}
You're welcome. I realised the 'tag' member overrides the inherited 'tag' member. You can simply rename 'tag' to anything like 'filterTag'.
Hi i attached your script to my teleport objects and it seem to work fine the only problem is whenever i enable "istrigger" on player it contrinues to fell below ground as its invisble to him i tried with "istrigger" enabled above object and it teleported m so do you know how to enable "istrigger" inside and collider and not make the player fall? It's in a 2d game i will appreciate any support
Hi after i added this script to my teleport (objects 1 and 2) it works but the problem is after i enable "istrigger" (In my box collider 2d) my player keeps falling even below the ground i know that that's what "istrigger" do but any idea how to fix it? Would much appreciate the feedback i am talking for 2d games BTW
If you set an object's collider to IsTrigger
, then it will act as a volume of space that can be entered (pass-through). Your player falls through everything because you made it a trigger and it has a rigidbody on it. You cannot use it as a trigger collider
Answer by ransomink · Mar 13, 2018 at 03:50 PM
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...
Hey, ranso$$anonymous$$k, I made a Unity 2018.1.6f1 conversion just in case. Just make sure that: - You've made the teleporter area, added a Box Collider 2D, and checked "Is Trigger". - You've made a seperate GameObject that is empty, named "Teleporter.Destination", and is not a child of the teleporter area. - You have a GameObject (that you want to be your player) tagged as Player.
// Variables
[SerializeField] private Transform teleport;
// Code
private void OnTriggerEnter2D(Collider2D other) {
if ( other.CompareTag( "Player" ) )
{
other.transform.position = teleport.position;
}
}
}
// End of File