- Home /
Rotate an Object based on shorter angle
Hello! Today I have been having the issue of getting my objects to rotate. My goal is to get my missile object to rotate to the enemy based on the shorter angle distance.
The Green line is my current angle (Local Y axis), and the red line is from my missile's center to the enemy's center, or the angle I want to rotate to.
My code currently: Also, as a note, the enemy is labeled as "target"
float myAngle = transform.rotation.z;
float targetAngle = Vector2.Angle (new Vector2(transform.position.x, transform.position.y), new Vector2(target.transform.position.x, target.transform.position.y));
print (myAngle - targetAngle + 20.5);
float rotateDeg = Mathf.Clamp(myAngle - targetAngle + 20.5f, -5, 5) / -5.0f;
//if(rotateDeg >= 0)
//transform.RotateAround(transform.position, Vector3.forward, turningRadius * Time.deltaTime * rotateDeg * 10);
//rigidbody2D.MoveRotation(targetAngle * Time.deltaTime * rotateDeg * 100);
rigidbody2D.AddTorque(rotateDeg * 20);
In my current attempt it works on small differences, but at a larger scale it spins out of control, also when it comes to the commented out attempts, they just didnt work, but it could be a a start if someone wants to try.
Answer by skylem · Jan 14, 2015 at 01:46 AM
u can use look rotation to determine a forward rotation between ur two objects
transform.rotation = Quaternion.LookRotation(transform.eulerAngles, target.transform.eulerAngles);
there is also
transform.LookAt(target.transform);
if neither of these work for u let me know and ill see what else i can dig up in my scripts.
Edit --
Vector2 dir = (target.transform.position - transform.position).normalized;
float direction = Vector2.Dot (dir, transform.forward);
and here is another method that smoothly transitions from one rotation to another
transform.eulerAngles = Vector2.Lerp(transform.eulerAngles, target.transform.eulerAngles, 0.01f); //from rotation A to Rotation B and the final value (float) is how many frames it will transition over.
Edit --forgot to include the way u would use ur direction value to determine which way to rotate would be to use an if to check the current direction before applying ur rotate -1 represents directly behind and 1 represents directly in front.
if(direction > 0) { // apply your forward rotation } else if(direction
Yeah, that's not working too well, what I was looking for is turning the rocket to face the target in the shortest distance, but smoothly, these seem to just snap it directly, except for the Quaternion one, that one just goes into a different dimension
Actually I got it to work with transform.Rotate(Vector3.forward Time.deltaTime turningRadius, Space.World);
Unfortunately, now the final stage is to get it to detect the ability to choose whether right or left is shorter to turn.
Thanks for that if Statement, you were very helpful! If I need any help with this I'll let you know! +++
Your answer
Follow this Question
Related Questions
Transform.Rotate() stuck at 90 and 270 degrees 3 Answers
Transform Rotate a certain angle (and back) 2 Answers
Rotate 90 over time on mouseDown 2 Answers
How can I rotate the transform by a specific angle at a certain speed? 2 Answers
Calc 30° left and x units forward from local forward axis 1 Answer