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
2
Question by SoxwareInteractive · Sep 15, 2016 at 12:45 PM · transformquaternion.lookrotationangleaxis

LookRotation with correct roll angle (using angle axis)

Hi everyone,

I have a UnityEngine.Transform which is rotated/oriented in any arbitrary way in 3D space. Also there is a 3D target point in world space. By using Quaternion.LookRotation() it's possible to have an object pointing from the transforms position to the target point. But I also want the pointing object to be be rolled in such a way, that it represents the transforms rotation angle ("roll / twist") along the axis between the transforms position and the target point:

alt text

This code doesn't show the correct roll angle as it doesn't take the transforms rotation into account yet:

 Vector3 dir = worldSpaceTargetPoint - transform1.position;
 
 pointingObjectTransform.position = transform1.position;
 pointingObjectTransform.rotation = Quaternion.LookRotation(dir, Vector3.up);

So how can I change the code to get the correct roll angle? I guess I would need a method like Quaternion.AngleAxis(float angle, Vector3 axis) that takes a quaternion and an axis as input parameters and returns the angle. Sadly I found no method for that... Thanks in advance!

lookrotation.png (3.8 kB)
Comment
Add comment · Show 4
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 Glurth · Sep 15, 2016 at 03:46 PM 1
Share

https://docs.unity3d.com/ScriptReference/Transform.LookAt.html and https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html

These function takes two parameters, the first is the direction you want to look, and the axis you mentioned in your question.

The second param defines a rotation around this axis, by specifying which direction is "up". Imagine you have a camera at the origin pointing along the axis, at the target. the "up direction" is the direction that the top of the camera's viewport points. In your example code you are using Vector3.zero, for this parameter, which is invalid input, since it does not define a direction. Usually people use Vector3.up for this param, but, of course, it varies by situation: I'm not quite clear what value you want to use in there, from your description.

Another commonly used second parameter is: transform.up for the second param (convert's the transform's local/modelspace "Up", to a world-space direction.)

avatar image SoxwareInteractive Glurth · Sep 15, 2016 at 05:19 PM 0
Share

Hi @Glurth,

thanks for your answer! I accidentally wrote Vector3.zero when I composed this simplified version of my real script. I corrected my original post. Sorry for that.

You are right, Transform.up would be exactly what I need and I already tried that but the problem is due to the fact that the transform can have any arbitrary orientation (rotation) in world space, it could be that transform.up is pointing in the same direction as the rotation axis. Which results in undesired rotations.

So maybe the real question is, how to get an up vector that is perpendicular to the viewing direction (target point - transform.position) and represents the correct "roll" angle (see picture above)? And to get this roll angle, I would need a method like Quaternion.AngleAxis(float angle, Vector3 axis) but which returns the angle and uses the axis and the quaternion as input parameters. Does someone know the maths behind this method?

avatar image Glurth SoxwareInteractive · Sep 15, 2016 at 08:26 PM 0
Share

"how to get an up vector that is perpendicular to the viewing direction (target point - transform.position) and represents the correct "roll" angle (see picture above)?" I'm unclear why transform.up is not sufficient for these needs. This is specifically different from Vector3.up, in (pretty-much, unless I misunderstand), the way you describe.

Edit: oh, I misread your answer- I think you see this already. Obviously I'm just not understanding what your looking for. What do you expect "up" to be, in the case you mentioned, where the transform's original up, happened to be pointing directly at the target?

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Finding roll angle 0 Answers

"Quaternion.LookRotation" not working when Instantiate 1 Answer

Quaternion.LookRotation() doesn't look down 2 Answers

Make object lay flat on a sphere + look up? 0 Answers

How do I smooth out my crouch camera movement 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