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 Xarbrough · Jun 16, 2015 at 11:40 PM · c#rotationmath

Rotation math problem

alt text

I have a camera that rotates around a disk. When I create an empty gameobject at the center of the disk and child the camera to it, I can simply rotate the whole thing so that the camera keeps its x-axis rotation (tilted up).

How do I replicate the exact same behaviour without using Unitys child-parent rotation?

I already have it working that I get the correct camera position on the "unit circle" via the degrees I input, but next, I need to rotate the camera so that it always looks slightly away from the circumference. How can I fix the line marked with "PROBLEM"?

 //center is 0,0,0
 
 protected void RotateBy(float degrees)
 {
     _angle += degrees;
 
     // Set new position
     float x = Mathf.Cos(_angle * Mathf.Deg2Rad) * _altitude;
     float y = Mathf.Sin(_angle * Mathf.Deg2Rad) * _altitude;
     transform.position = new Vector3(x, y, transform.position.z);
 
 }
 
 ///
 
 void LookAtCenter()
 {
     Vector3 directionToCenter = Vector3.zero - transform.position;
     float zRotation = Mathf.Atan2(directionToCenter.y, directionToCenter.x) * Mathf.Rad2Deg;
   
     // Offset 90 degrees to make bottom face the center.
     // PROBLEM: This only works if x and y are both 0
     transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, zRotation + 90f);
 }

rotationproblem.jpg (41.5 kB)
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 maccabbe · Jun 17, 2015 at 12:03 AM

It seems like the simplest solution would be to use Quaternion multiplication. It's what Unity (probably) uses for child-parent rotations. In your case you would do something like

  Quaternion tiltRotation=(Whatever you want it to be);
  Quaternion circleRotation=Quaternion.Euler(0, 0, zRotation+90f);
  transfrom.rotation=circleRotation*tiltRotation;

 

http://docs.unity3d.com/ScriptReference/Quaternion-operator_multiply.html

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.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Trouble with Camera Rotation Math 2 Answers

Rotation using exponential functions 1 Answer

Projectile motion arc 2d 1 Answer

How to Reverse an equation.. (c#) 2 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