How to get 2d character to move towards mouse?

Im expirementing with a little generator project and i want the characters(humans) to move towards where my mouse is on the x axis only, how can i do this?

There a lot of tutorials and solutions out there, but first you need to know on which space is your character in, Is your character in Canvas Screen Space , Canvas Camera Space , world space or NavMesh System.

once you find it out which space is your character in, then next
get the mouse position
https://docs.unity3d.com/2020.2/Documentation/ScriptReference/Input-mousePosition.html
mouse position is return screen space value pixel by pixel as vector3 type

then you convert it using Camera.ScreenTo What ever space you want
if your character is in world you may need ScreenPointToRay, ScreenToWorldPoint you will need z position for depth
https://docs.unity3d.com/2020.2/Documentation/ScriptReference/Camera.html

its should be easy to search for tutorials once you know which space is your character in.

keep in mind there is a big different between ā€œmove towards mouseā€ (direct coordinates movement) and ā€œfacing and move towards mouseā€ (facing and moving forward).

2 Likes