- Home /
Strange Problem with Torque-based Rotation
I am trying to get the arm of a character to face the mouse at all times.
The way I am doing this is using torque. The script will add more force the bigger the angle difference between the arm and the mouse is.
However, the arm doesn't seem to face the correct direction. The arm looks left when the mouse is left and right when the mouse is right, but it looks up when the mouse is below and viseversa, which is not suppose to happen.
Oddly enough when I try to use the angle difference * -1, the arm looks right when the mouse is left (and viseversa), but it looks up when the mouse is up.
In other words, using the normal angle difference, only the vertical direction is flipped, but when the angle difference is made opposite, only the horizontal direction is flipped.
I have never encountered such strange problem, and I have no idea what in my script is causing it. The game is a 2D game and this arm is attached to the player body using HingeJoing2D.
Here's my script as of now:
public class ArmController : MonoBehaviour { private Rigidbody2D rb; private HingeJoint2D pivot; private Vector2 curPos; private Vector2 pivotPos; private Vector2 direction; private float pivotAng; private float angleDif; // Use this for initialization void Start() { rb = GetComponent<Rigidbody2D>(); pivot = GetComponent<HingeJoint2D>(); Cursor.visible = true; UpdatePivot(); } private void FixedUpdate() { } // Update is called once per frame void Update() { UpdatePivot(); //update angle difference angleDif = VectorToAngle(direction) - pivot.jointAngle; //this is here to keep the angle between -180 and 180 angleDif = angleDif % 360; if (angleDif > 180) { angleDif = angleDif - 360; } else if (angleDif < -180) { angleDif = angleDif + 360; } TorqueAtMouse(); Debug.Log("cursor: "+ VectorToAngle(direction)); Debug.Log("pivot: " + pivotAng); Debug.Log("delta: " + angleDif); Debug.Log("angular velocity: "+ rb.angularVelocity); } /* * (delta >= 2 && delta <= 180) || (delta >= 362 && delta <= 540) || (delta >= 358 && delta <= -180) * (delta <= -2 && delta >= -180) || (delta <= 358 && delta >= 180) || (delta <= -362 && delta >= -540) */ protected void UpdatePivot() { pivotPos = pivot.transform.position; curPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); direction = curPos - pivotPos; /* pivotAng = pivot.jointAngle; if (pivotAng > 360 || pivotAng < -360) { pivotAng = pivotAng % 360; if(pivotAng > 180) { pivotAng = pivotAng - 360; }else if(pivotAng < -180) { pivotAng = pivotAng + 360; } //pivotAng - 360 * Mathf.Floor(pivotAng+180 / 360) }*/ } void TorqueAtMouse() { if ((angleDif >= 3 && angleDif <= 180) || (angleDif >= 363 && angleDif <= 540) || (angleDif >= 357 && angleDif <= -180)) { rb.AddTorque(-angleDif / 30); Debug.Log("Turning +"); } else if ((angleDif <= -3 && angleDif >= -180) || (angleDif <= 357 && angleDif >= 180) || (angleDif <= -363 && angleDif >= -540)) { rb.AddTorque(-angleDif / 30); Debug.Log("Turning -"); } else { rb.AddTorque(-rb.angularVelocity * rb.mass); } if (rb.angularVelocity > 1000 || rb.angularVelocity < -1000) { rb.angularVelocity = 0; } } protected float VectorToAngle(Vector2 vector) { return Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg; } }
Your answer
Follow this Question
Related Questions
Rigidbody2D keeps rotating when freeze position is checked 1 Answer
Rigidbody rotation: Use hinge joint motor or MoveRotation script? 0 Answers
Hingejoint2D makes a spin before getting into the angle limits. 1 Answer
,Rotating a 2D sprite with Input 0 Answers
How do I instantly change the direction of rotation of a gameobject? 1 Answer