• 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 Kaezon · Jan 25, 2014 at 08:50 AM · turret2d rotation

Rotate 2D turret toward target heading (LerpAngle)

I'm trying to write a script to rotate an object toward a specified heading using LerpAngle(). So far, it rotates, but it never stops rotating. The targetHeading acts more like a speed than a target.

Any idea how to get the object to hit that target heading and stick?

public class Turret : MonoBehaviour {

         public float TargetHeading; //0deg - 359deg
         public float RotateSpeed;
     
         // Use this for initialization
         void Start () {
             TargetHeading = 0;
         }
         
         // Update is called once per frame
         void Update () {
             //Map 0deg heading to a global rotation angle
             float zeroDeg = 0;
             float targetHTG = HeadingToGlobal (TargetHeading, zeroDeg);
             if (targetHTG != transform.rotation.z) {
                 float angle = Mathf.LerpAngle (transform.rotation.z, targetHTG, Time.time);
                 transform.Rotate(0,0,angle);
             }
         }
     
             //Convert a Heading value (deg) to a global rotation value (deg)
         float HeadingToGlobal (float headingDeg, float zeroDeg) {
             float result = zeroDeg - headingDeg;
     
             if (result < 0)
                 result += 360;
             else if (result >= 360)
                 result -= 360;
     
             return result;
         }
     }</pre>

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

2 Replies

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

Answer by robertbu · Jan 25, 2014 at 11:15 AM

I spot one serious issue. Transform.rotation is a Quaternion. They are non-intuitive, 4D constructs in which the individual values range from 0.0 to 1.0. When you refer to 'transform.rotation.z', you are not getting an angle rotation. What you may be able to use is 'transform.eulerAngle.z'. I say 'may' because eulerAngles has its own set of issues, but I think you can get away with using them here.

But it seems like you are may be going the long way around in this code. Here is an alternate solution that uses Vector3.right as the zero heading angle:

     public class Turret : MonoBehaviour {
     
         public float targetHeading; //0deg - 359deg
         public float rotateSpeed;
     
                 void Start () {
             TargetHeading = 0;
         }
         
             void Update () {
                 Quaternion q = Quaternion.AngleAxis(targetHeading, Vector3.forward);
                 transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * rotateSpeed);
             }
     }

If you want a different angle as your zero heading, you need to add a constant to targetHeading in the AngleAxis() call.

Comment
Add comment · Show 1 · 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 Kaezon · Jan 25, 2014 at 11:51 AM 0
Share

I went ahead and applied your suggestion and it's working perfectly. I guess I'll have to do some more studying on the subject.

Thanks for taking the time to help me out :)

avatar image
0

Answer by user135 · Mar 16, 2017 at 08:28 PM

 public Transform target;
 public float initialForwardAngle = 0; // initial angle of your "gun barrel"
 public float maxRotationSpeed = 60;
 public float threshold = 4;

 void Update() {
     RotateGradually2D ();
 }

 public void RotateGradually2D()
 {
     angleToTarget = Mathf.Atan2 (target.position.y - transform.position.y, target.position.x - transform.position.x) * Mathf.Rad2Deg;
     signToTarget = Mathf.Sign (angleToTarget - _currentAngle);

     if (Mathf.Abs(angleToTarget - _currentAngle) > threshold) {
         _currentAngle += signToTarget * maxRotationSpeed * Time.deltaTime;
     } else {
         _currentAngle = angleToTarget;
     }

     transform.eulerAngles = new Vector3(0, 0, _currentAngle - initialForwardAngle);
 }
 private float angleToTarget; // Destination angle
 private float _currentAngle = 0; // Current angle
 private float signToTarget;
Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Rotate Bullet with rotating Turret PROPERLY 2 Answers

The gameobject turns only once after colliding with another gameobject. 1 Answer

help with freezing 2D sprite rotation 2 Answers

How to rotate object x degrees on 1 key press with lerpangle 1 Answer

Creating a Mounted turret 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