- Home /
Rotate RigidBody2D to the cursor?
I have been trying multiple other scripts, but none work. I don't have any code myself, but what I need is a script that rotates a rigidbody2D towards the mouse position.
Comment
Answer by alireza97 · Sep 17, 2019 at 07:48 AM
this is a code I found from here. hope it helps
pvoid FixedUpdate()
{
float AngleRad = Mathf.Atan2(Input.mousePosition.y - transform.position.y, Input.mousePosition.x - transform.position.x);
float AngleDeg = (180 / Mathf.PI) * AngleRad;
rigidbody.rotation = AngleDeg;
}
This is for the 3D RigidBody component, it doesn't work for RigidBody2D. Or at least for me.
Sorry, have no idea how I didn't see 2D.
I edited my code hope this one helps.
Your answer