Teleport to where I click 2D

I’m creating a 2d game where I will not be able to move but to teleport.

I’ve made a 2d character (with no tags or scripts or anything)and attatched the camera to it (so the camera will follow the player). But I am not able to create a script that makes me able to tp to where you click.

If anyone can help please tell me.

Sorry for mt english btw.

Something like this?

public void Teleport(Transform tp_trans)
{
    Vector3 new_pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    new_pos.z = tp_trans.position.z;
    tp_trans.position = new_pos;
}

I’ve assumed that your Z position is constant.

Set your players tag to Player, then this script should do it. You can set this script to any object in the game too, as it checks the mouse position from the main camera, and it stores your player as a variable.

//Set up a variable to access the player from
private Transform player; 

void Awake()
{
//Find the player object and set it
player = GameObject.FindGameObjectWithTag("Player").transform;
}

void Update()
{
// Check if you click the left mouse button then change position
if (Input.GetMouseButtonDown(0))
player.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}