2D AddTroque towards Mouse Position

Hi I want my sprite object with rigidbody2D to rotate towards mouse.

I need to rotate Top Down SpaceShip. So I can`t use rotation function to rotate sprite instantly.
it has to slowly accelerate and slow down.

But how do I get that into variable?

I would somehow need to get some vector towards mouse or rly have no idea how to do this one. Would you help me please?

rigidbody2D.AddTorque(forceTowardsMouse);

2 Answers

2

I think you want to rotate your sprite toward mouse position. by using AddTorque() your sprite rotates at that direction but not stop. I think you want to rotate sprite toward mouse. so here is sample code this is not perfect but working.

public class RotateTowardsMoves : MonoBehaviour {
Vector3 mousePositionInWorld;

	float angle ;

	float startRotationOffset = 90;  //This is angle offset at starting.

	
	// Update is called once per frame
	void Update () 
	{

		mousePositionInWorld = Camera.main.ScreenToWorldPoint (Input.mousePosition);
		angle = ( Mathf.Atan2 (mousePositionInWorld.y - transform.position.y, mousePositionInWorld.x - transform.position.x) * Mathf.Rad2Deg);

		transform.eulerAngles = new Vector3(0,0 ,startRotationOffset + angle);

	}
}

Or You use LookAt with some modification for 2D object 1

Edit:

Replace this line

	transform.eulerAngles = new Vector3(0,0 ,startRotationOffset + angle);

With

	transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.Euler(0, 0, angle),speedFector * Time.deltaTime);

speedFector is any float value to rotate fast.(I tested as 2.0f).
It’s gives you effect like torque added. and also slow down rotation when reach to mouse position.

yeah, but thats why I wanted add torque not simple rotation because I am rotation top down spaceship, and it has to obey physical laws, so it cant stop in an instant. It has to kinda slow down by effect of "thrusters", which ofc isnt important here. but yeah I need addtorque to slowly increase rotation and slowly decrease rotation.

@Wrymnn Please look at my edited answer. I don't use AddTorque but use Lerp to get same effect.

Thanks mate :)

Good Day, i know that im approximately 6 years 2 late, i was searching for this solution for 2 frikin days and i think i’ve figured it out myself, i know that there might be more people that want to move with Add.Torque, so anyway:

 public class TowardsMousePlayer : MonoBehaviour
    {
        public float speed; // spin speed
        float angle1; // angle1 will be negative to normal angle
        public Rigidbody2D rb;
        float angle;     
    
    void FixedUpdate()
    {
        faceMouse();
    }

    void faceMouse()
    {
        Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);     
        angle = Mathf.Atan2(mousePosition.x - transform.position.x, mousePosition.y - transform.position.y) * 180 / Mathf.PI; //Get mouse angle

        rb.rotation %= 360; // dont remember why i did this but better dont remove it

        angle = (angle + rb.rotation); // Sum up rigidbody and mouse angle

        if (angle < 0) angle1 = 360.0f + angle;
        else angle1 = 360.0f - angle; // calculates negative angle
        if (Mathf.Abs(angle) > Mathf.Abs(angle1) && angle < 0)
        angle = angle1;
        if (Mathf.Abs(angle) > Mathf.Abs(angle1) && angle > 0)
        angle = angle1 * -1; // from my testing i found out that by writing these ifs rigid body stops doing awkward 360 turnadounds and spins trough closest path to mouse
        rb.AddTorque(-angle / 180 * speed);
    }
}

You might have noticed that there’s no drag and the reason i havent added it here because you can add drag in rigidbody component itself but feel free to add drag to the script yourself.