I’m new to unity its my 4day working on it . I was following a 2D platformer tutorial I followed every step but when the player enter the portal collider it don’t teleport the player to another position like in the video it don’t do anything.
Here is the code
class playerDetection : MonoBehaviour
{
[SerializeField]private Transform portalNextPosition;
private Vector3 offset;
private void Start()
{
offset = new Vector2(1, 0);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "player")
{
collision.transform.position = portalNextPosition.position + offset;
}
}
}
I tried debug a message when the player collide
>Solution :
from what i read this script is attached to your portal and you are trying to detect when the player colliders with your portal
using if this gameobject tag equals to a string
one of these issues may be happening
- Case sensitive
c# is a case sensitive language so if your player tag is Player and you typed player instead it won’t detect your Player so try and change your code to this
public class playerDetection : MonoBehaviour
{
[SerializeField]private Transform portalNextPosition;
private Vector3 offset;
private void Start()
{
offset = new Vector2(1, 0);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("Player"))
{
collision.transform.position = portalNextPosition.position + offset;
}
}
}
- set your portal colliders to triggers
in your code you are using OnTriggerEnter2D which as it says 2D colliders and triggers so if your object collider isn’t in 2D or isn’t set to trigger it won’t work
go to your portal collider and check istrigger box and make sure you are using 2D colliders
- didn’t even assign tag to your Player
Check if your player gameobject has the Player Tag so it can be detected game objects in defualt are untagged you have to assign tags manually