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 Gode14 · Sep 27, 2014 at 09:16 PM · rotation3drotatealign

Aligning a hovercraft to two axis of a transform

Right now I'm trying make the hovering vehicle align itself to the road beneath it but maintain freedom of steering (think of a car going over a twisting hill - it rolls and pitches according to the surface underneath it, but the yaw depends on the direction of the craft). The game is in 3D, obviously.

I think the problem in my current code is that it doesn't account for the relative rotation of the object, only the world rotation value? I'm not sure if that makes sense.

Here is the alignment code so far (please tell me if you need to see anything else!):

 if (Physics.Raycast (downRay, hit)) {
     var alignment = Quaternion.Euler(hit.transform.rotation.eulerAngles.x, 0, hit.transform.rotation.eulerAngles.z);
     transform.Rotate(alignment.eulerAngles * Time.deltaTime);
 }

On top of this, I can't get the model for the vehicle to be properly aligned. Right now to face forward the model is rotated 270 degrees on the x axis.

If you don't want to answer but could point me to something I could read to help me understand, that would be appreciated as well! Thank you.

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
0
Best Answer

Answer by robertbu · Sep 27, 2014 at 09:18 PM

The typical solution to this problem is to Raycast() and get the normal of the surface. Then you can do:

  var q = Quaternion.FromToRotation(transform.up, hit.normal); 
  transform.rotation = q * transform.rotation;

This will allow you to continue to rotate on the object's (local) 'y' while aligning with the surface. Note for what you are doing, you might want/need to sample the normal at the four corners of your craft and use the average.

Comment
Add comment · Show 3 · 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 Gode14 · Sep 27, 2014 at 09:36 PM 0
Share

After I changed the from to transform.forward because of my messed up model it works perfectly! Thank you so much! If I may, is there a simple way to make the rotation happen over time ins$$anonymous$$d of instantly? Or limit the amount of rotation that can happen per frame? If you just want to tell me the page in the script reference to read that would also be appreciated!

avatar image robertbu · Sep 27, 2014 at 09:41 PM 0
Share

You can limit the speed of the rotation by doing:

  var q = Quaternion.FromToRotation(transform.up, hit.normal); 
  q = q * transform.rotation;
  transform.rotation = Quaternion.RotateTowards(transform.rotation, q, Time.deltaTime * speed);

'speed' will be in degrees per second. For an eased rotation, replace 'RotateTowards' with 'Slerp', and greatly reduce 'speed'.

avatar image Gode14 · Sep 27, 2014 at 10:02 PM 0
Share

Thank you so much!

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

2 People are following this question.

avatar image avatar image

Related Questions

Smoothly rotating an object to a specific rotation. 1 Answer

How to Reset Quaternion/Rotation On 3D Object After Dragging Mouse? 2 Answers

How to smoothly rotate an object on only two axes? 2 Answers

Rotate camera only in 2 directions based on player touch 1 Answer

Cant align players up vector to surface normal 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