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 /
avatar image
1
Question by Reconnoiter · Aug 10, 2017 at 11:57 AM · rotationvector3directionlocal

rotate vector around local axis

Hello,

I am trying to rotate a vector3 ( new Vector3 (0, 0, dist) ) around 3 axes locally.

-Edit2: see comments below -Edit: I think I am very close to it but something goes wrong the axes I think:

 Vector3 _vec = new Vector3 (0, 0, dist);
 _vec = Quaternion.AngleAxis(_rotYaw.y, Vector3.up) * _vec; 
 _vec = Quaternion.AngleAxis(_rotationVec.x, Vector3.right) * _vec; 
 _vec = Quaternion.AngleAxis(_rotationVec.z, Vector3.forward) * _vec; 
 

Here is a pic (the 3d sphere is just there to visualize the rotation, the yellow and green lines are important, the green line is where the vector should point to or whatever how that is called):

alt text

Comment
Add comment · Show 1
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 Reconnoiter · Aug 12, 2017 at 12:54 PM 0
Share

Pretty please with sugar on top, please help, I just want to know how to rotate vector3 around axes locally.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by FlaSh-G · Aug 12, 2017 at 01:10 PM

I don't know where the problem is with your code (not saying there is none), but why not just put all rotations into one quaterion and then multiply it?

 Quaternion.Euler(_rotationVec) * _vec
Comment
Add comment · Show 10 · 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 Reconnoiter · Aug 12, 2017 at 03:22 PM 0
Share

Hi Flash-G, I tried that but doesn't give the desired effect. I want rotation around the axes locally, e.g. similar to rotation in flight/spacesims. $$anonymous$$ore specifically; pitch and roll world-rotation like and jaw local-rotation like. Not sure if I am using the right definitions, otherwise I can try to elaborate it more if it is still unclear.

avatar image FlaSh-G · Aug 12, 2017 at 03:50 PM 0
Share

Oh, then you shouldn't define your rotation with three axes. That's basically an euler angles vector. The (or one?) reason Unity doesn't use that for rotations internally (ins$$anonymous$$d using Quaternions) is because euler angles can cause a gimbal lock.

How about you have a Quaternion (maybe transform.localRotation already is that) and multiply it with other Quaternionswhen input happens?

avatar image Reconnoiter FlaSh-G · Aug 14, 2017 at 11:52 AM 0
Share

I forgot to mention something, the jaw rotation is stored in an other Quaternion. But I am almost there with your advices but I now only still have a problem when roll != 0 and also when pitch is negative (so it now only works when roll == 0 and pitch >= 0 ):

 Vector3 _vec = new Vector3(0, 0, dist); 
 _vec = Quaternion.Euler(new Vector3(_rotationVec.x, 0, _rotationVec.z)) * _vec; //add pitch/roll to direction
 _vec = Quaternion.AngleAxis(_rotYaw.eulerAngles.y, Vector3.forward) * _vec; // add jaw (note that I tried Vector3.upward too but that does nothing)
avatar image Reconnoiter FlaSh-G · Aug 15, 2017 at 06:30 PM 0
Share

Hi @FlaSh-G , do you perhaps know why the following code works but only when _rotationVec points forwards or backwards, but not when it points to the left or right? (I think it has to do with Vector3.forward in RotateAround but not sure);

 Vector3 _vec = new Vector3(0, 0, dist);                                 
 _vec = Quaternion.Euler(new Vector3(_rotationVec.x, 0, _rotationVec.z)) * _vec; //add pitch/roll to direction
 GameObject _rotGO = new GameObject();
 _rotGO.transform.position = _vec;
 _rotGO.transform.RotateAround(Vector3.zero, Vector3.forward, _rotYaw.eulerAngles.y); //add local yaw rotation
 _vec = _rotGO.transform.position;
 Destroy(_rotGO); //clean up
avatar image Glurth Reconnoiter · Aug 15, 2017 at 06:50 PM 0
Share

You mentioned that you want to affect the local-space rotation of the object, but it looks like you are changing the WORLD-space rotation of the object with:

 _rotGO.transform.RotateAround

I suspect you want to adjust _rotGO.transform.localRotation ins$$anonymous$$d. This will probably simplify things sufficiently that you can use a single Quaternion.Euler to generate that quaternion.

Show more comments
avatar image FlaSh-G FlaSh-G · Aug 15, 2017 at 06:57 PM 0
Share

To be honest, at a certain point rotation code becomes very hard to follow for me. I suspect it's like that for many people. Anyway... It should be possible to set the rotation once ins$$anonymous$$d of rotating the thing and then rotating it again. Even more so, it should be very possible without doing the expensive action of creating a GameObjecta and then deleting it again.

I recommend getting familiar with the methods Quaternion has to offer and see if what you're trying to accomplish hasn't been conveniently bundled for you already.

avatar image Reconnoiter FlaSh-G · Aug 17, 2017 at 02:17 PM 0
Share

Hi, I already tried most of those functions but there doesn't seem to be anything that lets me enter the rotation values as euler angles while actually rotating it around the axis ins$$anonymous$$d of standard rotation. Very frustrating. :/ Quaternion.AngleAxis (in my first post) seems like the solution but it just didn't work for me for some reason. Anyway thanks for your time.

avatar image
0

Answer by Gandarufu · Jun 10, 2020 at 09:31 AM

Is this still an issue? I solved a similar problem with the following code:

 transform.Rotate(Vector3.up, mouseDelta.x, Space.Self);
 transform.Rotate(Camera.main.transform.right, mouseDelta.y, Space.World);

This results in a rotation similar to the prefab preview window in the inspector. Maybe the code above helps.

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

102 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 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 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

Rotate vector3 relative to another vector3 1 Answer

Rotate object based on hit normal + matching camera direction 0 Answers

Mesh won't travel in direction it's facing 1 Answer

Local FPS Gravity Changing Lerp 0 Answers

Waypoint System Does Not Rotate Enemy? 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