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
3
Question by J_P_ · Jul 26, 2013 at 01:13 AM · rotationquaternioneuleranglesrollalign

How to smoothly align an object to a surface, but only partially

So I have an object and I want to align it to a surface, but still give freedom of movement along some of the axises. I have two behaviors I want (also made a video, linked below, to help explain).

alt text

In some instances, I want the roll to be aligned to the surface and allow freedom of rotation along the pitch and yaw (scenario 1) -- this would let users look up/down and left/right relative to the surface (imagine FPS mouselook, but standing on a wall). In other instances, I want the roll and pitch to be aligned to the surface and only allow freedom of rotation along the yaw (scenario 2) -- this would let users only look left/right along the surface.

I need to be able to smoothly transition from/to arbitrary rotations, so for Scenario 1, I'm thinking I'd need to rotate along the roll axis (euler z) until the roll is lined up with the surface horizon. This is where I get totally lost. How would I even measure how far off the roll is from the surface horizon (knowing the pitch/yaw could be anything) so that I can smoothly roll the object into alignment?

Video to help describe what I'm trying to do: http://www.youtube.com/watch?v=VFVCiFz1STE (demonstrates Scenario 1 -- I'm hoping after solving scenario 1, it wouldn't be too hard to solve scenario 2)

[2]: http://forum.unity3d.com/threads/33987-Character-align-to-surface-normal

alignimage.jpg (100.6 kB)
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 Seth-Bergman · Jul 26, 2013 at 01:17 AM 0
Share

first off, great post, very clear (upvoted)

now, does the surface change orientation, or is it simply parallel to the xz plane at all times? in other words, will the object need to continuously check the orientation of the particular surface below it (such as a terrain map, versus a simple plane)?

avatar image J_P_ · Jul 26, 2013 at 01:29 AM 0
Share

@Seth_Bergman - The surface won't change orientation -- it's safe to assume the surface is always a flat plane :)

2 Replies

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

Answer by Seth-Bergman · Jul 26, 2013 at 01:28 AM

I would do it this way:

add an empty game object to each of the four "corners" of your plane (nose, tail, each wingtip)

do a raycast straight down in world space from each wingtip, and compare the distance to the ground.. if the right is higher, it will return a greater distance.. then you can rotate the object around its own (z?) axis until they are even.. the same method should work with the nose/tail for the pitch I believe.

EDIT:

in your case, however, this may be overkill.. if the surface is always flat, we can probably just use a parent object with the default orientation, for each rotation...

i.e. an empty gameobject which you parent the plane to, and apply ONLY z rotation to this object.. then just keep the parent oriented.. if that makes sense..

okay, maybe that's not clear enough..

by separating each axis of the rotation to a separate gameobject in the hierarchy, we can achieve the same end result without the need for complex rotation calculations.. so the hierarchy could be like :

empty parent(z rotation only)>child empty(y rotation only)>child plane(x rotation only)

by keeping each one separate, adjustments in general become much more manageable imho

since you took the time, here's what I mean:

alt text


planeexample1.jpg (63.8 kB)
planeexample1.jpg (63.8 kB)
Comment
Add comment · Show 2 · 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 J_P_ · Jul 26, 2013 at 01:37 AM 0
Share

That makes a lot of sense -- it's usually the way I solve things (in a more physical, rather than raw mathematical, sense) haha. I'll give it a shot tomorrow. I think ideally it'd be done through math, but that sounds like a solid backup plan.

avatar image Seth-Bergman · Jul 26, 2013 at 01:45 AM 0
Share

I've used similar tactics in past projects, to get vehicles to conform to terrain, so it is definitely viable.. But I got nothin against maths :)

avatar image
1

Answer by robertbu · Jul 26, 2013 at 06:40 AM

If 1) you can make the object a child of the reference plane (not strictly necessary), and 2) if you have a central place in your code where you make yaw, pitch, and roll changes, and 3) if your code directly makes the rotation changes (i.e. the object is not getting tossed around by Rigidbody forces), then this problem becomes easy. Make your object a child of the reference plane and encode your yaw, pitch and roll in a Vector3. The Vector3 is then assigned to Transform.localEulerAngles. The trick to making this work is to never read Transform.eulerAngles or Transform.localEulerAngles. All calculations are placed in your Vector3 and then assigned to Transform.localEulerAngles.

If you don't want to make the object a child, you can just multiply the rotation by the rotation of the reference plane. Something like:

 transform.rotation = refPlane.transform.rotation * Quaternion.Euler(myRotationVector3);


Scenario 2 is also easy for the general case (i.e. no Vector3 and object impacted by forces).

 Quaternion q = Quaternion.FromToRotation(refPlane.transform.up, transform.up);
 transform.rotation = q * transform.rotation;
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

16 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

Related Questions

ConfigurableJoint - angular positions (rotation) problem 1 Answer

EulerAngles conversion Quaternion problem 2 Answers

Mouse Orbit on a Different XY Plane? 2 Answers

Rotation Jumping values (0 to 180) 1 Answer

Is there no way to get reliable angles from a rigidbody? 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