Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Phoenix918 · Aug 11, 2015 at 11:44 PM · c#vector3camera-movementcamera rotate

Adjust camera orientation through code

I am working on a game where the camera needs to focus on a mountain at the center of an island. The camera works by rotating (using Transform.RotateAround) around a point within that mountain. The camera also needs to pan right and left, which I do by Translating it on its local left and right vectors. However, once you use enough of Translate and RotateAround, the camera becomes tilted to the side, and it gets progressively worse as you play.

I have tried correcting it by running this function every frame:

 private void StraightenCamera()
 {
     //tilt it upward so it is always oriented right
     //goal: make the local up vector lie in the plane defined by the local forward and global up vector
     //http://www.vitutor.com/geometry/distance/line_plane.html
     Vector3 localForward = this.transform.TransformDirection(Vector3.forward);
     Vector3 globalUp = Vector3.up;
     Vector3 u = this.transform.TransformDirection(Vector3.up);
     Vector3 n = Vector3.Cross(localForward, globalUp);
     float numerator = Mathf.Abs(n.x * u.x + n.y * u.y + n.z * u.z);
     float denominator = Mathf.Sqrt(n.x * n.x + n.y * n.y + n.z * n.z) + Mathf.Sqrt(u.x * u.x + u.y * u.y + u.z * u.z);
     float angle = Mathf.Rad2Deg * Mathf.Asin(numerator / denominator);
     this.transform.Rotate(Vector3.forward, angle, Space.Self);
 }


This function mostly works, however there is a catch. It'll rotate the camera to the correct orientation after a few transformations, and then it'll decide to rotate the wrong way until it corrects to be perfectly upside down from where it should be. Mess around with it a little bit more and it'll correct itself to the right orientation, but as you continue to play it'll get confused and flip upside down again. I don't understand what is triggering the flipping, or where the problem in my math is. Does anyone see it?

Explanation of the above function: My thinking was that as it reorients itself, its local up vector was getting tilted. I don't care where it points, so long as it is in the plane defined by the local forward vector (wherever the player may have pointed that), and the global up vector. I am rotating the camera around its local forward axis in an attempt to maintain this condition. One hint that my math is off is that it doesn't correct this misalignment in a single frame like I intend, but it does so gradually, either when it is correcting to the correct orientation or upside down. The website I got the formula from is in a comment in the function.

Video of the problem: https://youtu.be/J3a__u8vrYo

Thank you for any help you may be able to provide!

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 getyour411 · Aug 12, 2015 at 04:28 AM 0
Share

I almost never comment on Rotation questions because I'm hopeless with those, but one note from LateUpdate() doc says use it for Camera stuff, so maybe try calling this from LateUpdate() assu$$anonymous$$g it's currently in Update().

http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.LateUpdate.html

avatar image Phoenix918 · Aug 12, 2015 at 05:19 AM 0
Share

I tried it out but it made no difference. Thanks for the suggestion though.

0 Replies

· Add your reply
  • Sort: 

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

24 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

Related Questions

Allow both turning of the mouse and turning of the player to keep the camera pointing towards the player 1 Answer

Camera Clamping Third Person? 0 Answers

Multiple Cars not working 1 Answer

Lean/Peak system 1 Answer

Confining Mouse Look on X axis 0 Answers


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