• 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 Pitcher · Oct 22, 2015 at 08:14 AM · 2d rotation

Quaternion.RotateTowards script working for one object but not for another.

Hi everyone,

Very new to all this and doing my best to find my own way through but this one has me stumped.

In a 2D orthographic game I have two search lights running the same script designed to rotate them, back and forwards, continuously between two angles around the z axis. There are three public variables, the turn rate and the max and min angles. These last two set the limits of the search arc. Certain angles seem to work, such as maxAngle = 320 and minAngle = 5 but most others do not.

I've put the code below and I'd really appreciate some fresh eyes and a nudge in the right direction.

 public class SearchLight : MonoBehaviour {
 
     public int maxAngle;
     public int minAngle;
     public float turningRate;
 
     private Quaternion _targetRotation;
     private bool changeDirection;
     private Vector3 targetMaxAngle;
 
 
     // Use this for initialization
     void Start () {
         changeDirection = false;
         _targetRotation = Quaternion.identity;
         targetMaxAngle.z = maxAngle;
     }
 
     // Update is called once per frame
     void Update () {
 
         SetBlendedEulerAngles(targetMaxAngle);
         transform.localRotation = Quaternion.RotateTowards(transform.localRotation, _targetRotation, turningRate * Time.deltaTime);
 
         if (changeDirection == false)
         {
             targetMaxAngle.z = maxAngle;
             if (transform.localRotation.eulerAngles.z == targetMaxAngle.z)
             {
                 changeDirection = true;
             }
         }
 
         if (changeDirection == true)
         {
             targetMaxAngle.z = minAngle;
             if (transform.localRotation.eulerAngles.z == targetMaxAngle.z)
             {
                 changeDirection = false;
             }
         }
 
     }
 
     public void SetBlendedEulerAngles(Vector3 angles)
     {
         _targetRotation = Quaternion.Euler(angles);
     }
 
 }

My aim is to be able to attach this script to any search light, sets its variables and let it get on with searching. My confusion is why does it work with one and not another?

Thank you very much indeed,

Pitcher

Comment
Add comment · Show 2
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 Bonfire-Boy · Oct 22, 2015 at 10:23 AM 0
Share

A couple of things jump out at me.

The first is that you're using exact comparison of floats, in for example

 if (transform.localRotation.eulerAngles.z == target$$anonymous$$axAngle.z)

Exact comparison of floats is unlikely to succeed unless you're explicitly setting them to be identical somewhere, and it's not clear to me that you are.

The other thing is that you don't say what the unwanted behaviour is. Just saying that "most angles don't work" is leaving out some crucial information.

avatar image Pitcher · Oct 22, 2015 at 01:05 PM 0
Share

Thanks for getting back to me,

When I debug those values, they match up exactly hence why I used the exact comparison. Is it worth using a different method to avoid potential problems?

$$anonymous$$y apologies, to define the limits of the arc made by the search light I'm using maxAngle and $$anonymous$$Angle. Certain values allow the light to rotate back and forth between those limits, other values do not. The unwanted behaviour is when using certain values for maxAngle and $$anonymous$$Angle, the light won't repeatedly rotate between the two, ins$$anonymous$$d it will move towards maxAngle and stop.

For example, as above, maxAngle = 320 and $$anonymous$$Angle = 5 works. The light starts at 0 degrees, rotates to 320 degrees around the z axis, then changes direction to 5 degrees, then back again and so on indefinitely.

Change $$anonymous$$Angle to anything above 12, however, and the light will simply sit at 320 degrees and stay there.

I hope that helps, and apologies if I'm being unclear. It's difficult to explain when you don't fully understand anyway!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Pitcher · Oct 22, 2015 at 03:29 PM

Solved thanks to your hint on comparing exact values.

It was indeed causing problems so compared them using Mathf.Approximately and all is working now. Not sure why certain angles worked with the exact comparison but at least it's sorted now.

Thank you very much Bonfire Boy.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotate an animated gameobject (2D) 1 Answer

Changing 2d Destination by angle 1 Answer

Trying to achieve smoothness in drift like sling drift? 3 Answers

How can my sprite move to new direction it's facing not keep going to old direction? 0 Answers

2D rotate 90 degrees issue 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