Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by AdvancedGir · Nov 30, 2015 at 12:43 PM · rotationmanual

Rotate Vector3 Manually?

Hi all, i need to find a way to reproduce unity3 rotation on a simply server that dosn't have fancy unity3 functionality.

So far i have come up with:

public static Vector3 Rotate(Vector3 x, float _x, float _y, float _z) { float angle = _z; x = new Vector3( x.x * Mathf.Cos(Mathf.Deg2Rad * angle) - x.y * Mathf.Sin(Mathf.Deg2Rad * angle), x.x * Mathf.Sin(Mathf.Deg2Rad * angle) + x.y * Mathf.Cos(Mathf.Deg2Rad * angle), x.z); angle = _y; x = new Vector3( x.x * Mathf.Cos(Mathf.Deg2Rad * angle) + x.z * Mathf.Sin(Mathf.Deg2Rad * angle), x.y, -x.x * Mathf.Sin(Mathf.Deg2Rad * angle) + x.z * Mathf.Cos(Mathf.Deg2Rad * angle)); angle = _x; x = new Vector3( x.x, x.y * Mathf.Cos(Mathf.Deg2Rad * angle) - x.z * Mathf.Sin(Mathf.Deg2Rad * angle), x.y * Mathf.Sin(Mathf.Deg2Rad * angle) + x.z * Mathf.Cos(Mathf.Deg2Rad * angle)); return x; }

http://pastebin.com/LnMBdxEM

However i can't reproduce rotation angles that i input from transform.rotation.eulerAngles, on some angles it works on some not, any idea why?

Comment
Add comment · Show 3
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 paranoidray · Nov 30, 2015 at 01:22 PM 0
Share

Why not try something like this:
http://www.codeproject.com/Articles/7023/Sharp-D-$$anonymous$$ath-A-D-math-library-for-NET
or
http://stackoverflow.com/questions/607254/good-library-for-3d-math-in-c

avatar image AdvancedGir paranoidray · Nov 30, 2015 at 01:40 PM 0
Share

I try to reproduce unity3d rotation while i only have access to basic functions like sin and cos. Basically i can't use libraries here.

avatar image Fattie AdvancedGir · Nov 30, 2015 at 07:15 PM 0
Share

If you're working on some project that is so lame that you only have "access to sin and cos" .. forget the payment and move on to another job.

3 Replies

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

Answer by Bunny83 · Nov 30, 2015 at 05:52 PM

This one will exactly replicate the rotation that a Transform experiences with the given worldspace eulerangles:

 public static Vector3 Rotate(Vector3 aVec, Vector3 aAngles)
 {
     aAngles *= Mathf.Deg2Rad;
     float sx = Mathf.Sin(aAngles.x);
     float cx = Mathf.Cos(aAngles.x);
     float sy = Mathf.Sin(aAngles.y);
     float cy = Mathf.Cos(aAngles.y);
     float sz = Mathf.Sin(aAngles.z);
     float cz = Mathf.Cos(aAngles.z);
     aVec = new Vector3(aVec.x * cz - aVec.y * sz, aVec.x * sz + aVec.y * cz, aVec.z                    );
     aVec = new Vector3(aVec.x                   , aVec.y * cx - aVec.z * sx, aVec.y * sx + aVec.z * cx );
     aVec = new Vector3(aVec.x * cy + aVec.z * sy, aVec.y                   , -aVec.x * sy + aVec.z * cy);
     return aVec;
 }

So those two lines will result in the same vector:

 Vector3 input; // some vector
 
 Vector3 v1 = transform.TranslateDirection(input);
 Vector3 v2 = Rotate(input, transform.eulerAngles);


As others have said the order in which you apply the rotations matters. Unity uses the local order Y - X - Z. The worldspace order is always the reverse, so Z - X - Y.

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 AdvancedGir · Dec 01, 2015 at 06:06 PM 0
Share

Thank you for an excellent answer.

avatar image
1

Answer by DavidBlanpied · Nov 30, 2015 at 03:31 PM

What you're trying to do is like rotating the bits in a bitmap on the surface of a cube in 3-space. It works if the cube's surface is aligned with the global X-Y plane, but not generally. General rotations require quaternions, to be generally useful your Rotate() function needs to use quaternion multiplication.

Try this experiment: rotate the vector (0,0,1) by (90°,45°,45°). 1st, rotate 45° about the Z axis. In Yaw/Pitch/Roll terms, a roll. Still at (0,0,1). 2nd, rotate 90° about the X-axis, a pitch. Now we're at (0,1,0). Last, rotate 45° about the Y axis, another roll. Still at (0,1,0). This might not be the anticipated result. So the question is what does a rotation by (90°,45°,45°) mean? Rotate 45° about an axis tilted (90°,45°,0)? By Yaw = 90°,Pitch = 45°, Roll = 45°? Rotate 1st about X then Y then Z? They all give different answers.

The transformation first Z then X then Y is the transform.rotation.eulerAngles() transform using degrees and is equivalent to

 Quaternion.Euler(0, 0, z) * Quaternion.Euler(x, 0, 0) * Quaternion.Euler(0, y, 0)

so to duplicate transform.rotation.eulerAngles(), the Rotate() function in psudocode might look like

 return x * Quaternion.Euler(0, 0, _z) * Quaternion.Euler(_x, 0, 0) * Quaternion.Euler(0, _y, 0);

Other transforms are useful, for a camera azimuth/altitude transformation try

 transform.rotation = Quaternion.Euler(0, Az, 0) * Quaternion.Euler(-Alt, 0, 0);

or Yaw/Pitch/Roll:

 transform.rotation = Quaternion.Euler(0, Yaw, 0) * Quaternion.Euler(Pitch, 0, 0) * Quaternion.Euler(0, 0, Roll);

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
avatar image
0

Answer by Fattie · Nov 30, 2015 at 07:14 PM

The others have given you the full answer, but you will have to dramatically improve your understanding of quaternions before you can work in this realm.

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

35 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[C# Scripting] Set the rotation of the player, when another script access it at the same time. 0 Answers

How to rotate parent only without affecting children(in editor) 2 Answers

Instantiated Object Has Wrong Rotation 0 Answers

Rotation and Gravity Relative to the Center of an Object 0 Answers

With VR character's rotation is applied twice or double 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