2d game: player position should be mouse / touch position

Hey,
I am making a 2d game with c#.
My player should always move to the touch / mouse position. So my player should “glue” on the finger / mouse, that i can take the player with me.
If you have a script or a link where it is described, please let me know.
Thank you for your attention

// Update is called once per frame
void Update()
{
Camera cam = Camera.main;
Vector3 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
Vector3 destination = new Vector3(mousePos.x, mousePos.y, cam.nearClipPlane);
if (transform.position != destination)
{
transform.position = destination;
}
}

this will get you started. this script will lock the player’s transform to the mouse position. If you want smooth movement/damping you will have to Lerp the player to the destination variable instead of tracking it the way I did above. this method should work exactly the same via touch control if you supply the touch position in place of the mouse position.

Do you have anything you are working with? Are you running into any issues with the code or tool you are choosing to use? I haven’t looked, but I’m sure you can find something on the Unity Asset Store that does that for you for free.

Camera.main.ScreenToWorldPoint(Input.mousePosition) does that basically, you might want to check this out: Unity - Scripting API: Camera.ScreenToWorldPoint

If you are serious about making your own games, you need to learn how to code or find the tools that will help you on your own; game design and coding are both similar to solving puzzles, and you are basically asking for someone to come solve a pieces of your puzzle for you.

Without having a starting point or a tool that you are having trouble understanding, even giving you a solution will lead to questions of “How does this even work?”.

Sorry for the lecture, but game design and coding are both extremely hard to learn in the beginning, but the learning curve gets easier and easier the more you teach yourself, and with the access to YouTube and Udemy, there really isn’t any reason not to start learning the fundamentals yourself.