Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Limesta · Jan 14, 2015 at 12:05 AM · transformrotateangle

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.

alt text

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Limesta · Jan 14, 2015 at 02:27 AM 0
Share

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

avatar image Limesta · Jan 14, 2015 at 02:34 AM 0
Share

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.

avatar image skylem · Jan 14, 2015 at 03:06 AM 1
Share

ill add a method of direction checking to my answer.

avatar image Limesta · Jan 14, 2015 at 04:29 PM 0
Share

Thanks for that if Statement, you were very helpful! If I need any help with this I'll let you know! +++

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges