Making character move towards mouse.

Hello, I have a character with a rigidBody2D and I’m trying to make it move towards my mouse.

var camPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
camPos = Vector2(camPos.x, camPos.y);
var angle = Vector2.Angle(rigidbody2D.position , camPos);

rigidbody2D.rotation = rigidbody2D.rotation + angle;

Doesn’t work. The angle var is just random.

I’ve put another object on top of

var camPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
camPos = Vector2(camPos.x, camPos.y);

and that gets the mouse position flawlessly.

Try setting it to this instead:

rigidbody2D.rotation = angle;
rigidbody2D.AddRelativeForce(Vector2.forward * speed, ForceMode2D.Force);

The var angle isn’t random you are just doing current rotation plus angle. So it keeps adding angle to the current rotation.