• 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 joedrigon · Sep 19, 2010 at 04:59 AM · rotate

Camera Rotation Along Z Axis

I'm just trying a simple rotation of a camera starting at 180 degrees and want it to stop at exactly 0 degrees. I can't seem to get it to work properly.

Here is what I have so far that sort of works but it never stops at 0 it is always a degree or two off.

void Update() { // Start camera at a point and zoom it back if (Camera.main.orthographicSize != orthographicSize) { Camera.main.orthographicSize += 1.0f; }

 // Rotate camera counter clockwise
 if (Camera.main.transform.rotation.z > rotationEnd)
 {
     Camera.main.transform.Rotate(0, 0, -2.0f);
 }

}

If I try this other way the camera never stops spinning since it never hits exactly 0... so I have cheated for now and done it the way I have above.

    // Rotate camera counter clockwise
    if (Camera.main.transform.rotation.z != rotationEnd)
    {
        Camera.main.transform.Rotate(0, 0, -2.0f);
    }
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
2
Best Answer

Answer by Jesse Anders · Sep 19, 2010 at 08:14 AM

There's at least a couple of problems here. The first is that you appear to be expecting the 'rotation' property to return Euler angles, but 'rotation' is a quaternion. If you want to access the angles in Euler-angle form, you can use 'eulerAngles' or 'localEulerAngles'.

Now, it just so happens that in this particular case the code will still produce results that are sort of correct. When you initialize the orientation to a 180-degree rotation about the z axis, the 'z' element of the quaternion is set to 1, or sin(180/2). As the angle of rotation gets smaller, this value decreases. Presumably, the z value eventually becomes negative and rotation stops; however, in most cases you probably will have overshot the target rotation by some small amount.

The other part of the problem is that when working with floating-point numbers, you can't always expect a sequence of operations to produce the expected 'real number' result. This is especially true when transcendental functions are involved (as is the case here).

You can accept a certain degree of inaccuracy in your result, or you can use a deterministic method to ensure that the rotation ends where you want it to. For example, instead of rotating incrementally, you could use Quaternion.Slerp() to interpolate between the starting and ending orientations over time. Or, you could use Mathf.LerpAngle() or SmoothDampAngle() to accomplish the same thing. Other options include detecting when the object has 'passed' the target orientation and 'snapping' to the target orientation at that point. (There are various ways you could determine that the target orientation has been passed, but note that you'll need to take Unity's conventions for representing Euler angles into account when performing a direct comparison using angles.)

Comment
Add comment · Show 3 · 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 Eric5h5 · Sep 19, 2010 at 09:01 AM 0
Share

Also the code is framerate-dependent...you can't use values like 1.0 or -2.0 per frame and have it behave properly.

avatar image joedrigon · Sep 19, 2010 at 02:38 PM 0
Share

@Jesse This really helps me understand what the heck is going on with the angles... I appreciate the info.

@Eric5h5 I tried using it with Time.DeltaTime a couple of days ago and it just made the over shoot worse but I did have it in my code at one point.

avatar image Daniel Girgis · Jan 31, 2011 at 10:13 AM 0
Share

if u use Time.DeltaTime u gotta reduce the number by a fair bit

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

1 Person is following this question.

avatar image

Related Questions

Camera rotation around player while following. 6 Answers

How do I rotate object smoothly 3 Answers

How to make object lean in the direction it's going 3 Answers

Why does transform.Rotate( x, y, z ) work correctly but transform.Rotate( Vector3.forward ) does not? 1 Answer

Sidescroller Turning 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