• 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 Chris Jenkins · Mar 27, 2011 at 12:37 AM · camerarotationaxisalign

Aligning to a specific axis (6 DOF Descent-like movement)

Hello, I've been trying to figure out how to replicate a movement of a ship like in the game Descent 1. What I understand is that it is a 6 DOF camera and they have some sort of auto-leveling implemented.

Right now I have 6 DOF movement in my game, but I cannot figure out how to get the ship to align to the up vector on each axis. For instance with this code:

/* Retrieve Input */ float yaw = Input.GetAxis("Mouse X") * speed; float pitch = Input.GetAxis("Mouse Y") * speed;

/ Get the axis rotation by raycasting a normal to the surface / RaycastHit hitInfo;

if (Physics.Raycast(transform.position, -Vector3.up, out hitInfo, 400.0f)) { Quaternion rot = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);

/ This is fine only if I do not rotate my ship 45 degrees upwards (or vice versa). But, I must support aligning to the floor in all 6 directions (including upside-down). / transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y, rot.z, transform.rotation.w);

/ transform.rotation = rot; // Does not allow me to rotate my camera at all. transform.rotation = rot; // Did not work. */ }

/ Rotate camera / transform.Rotate(Vector3.up, yaw); transform.Rotate(Vector3.left, pitch);

This code will not align my ship properly in all rotations performed by the mouse. Is there some way to get this ship to align to the walls of my level no matter the rotation of the ship?

I'd appreciate any help, thanks.

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

Answer by Jesse Anders · Mar 27, 2011 at 06:08 AM

This:

transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y,
         rot.z, transform.rotation.w);

Won't do anything meaningful. The x, y, and z elements of the quaternion don't correspond to angles, and mixing and matching elements from different quaternions like that will just produce a more or less random result in the general case. (It might work out in some cases, but if so, it's just circumstantial.)

This:

transform.rotation *= rot;

Is close, except it needs to be:

transform.rotation = rot * transform.rotation;

This will align the object instantly to the surface normal. If you want the alignment to be more gradual, you can use Quaternion.Slerp() instead.

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 Chris Jenkins · Mar 27, 2011 at 11:17 AM 0
Share

Thanks, that works!

avatar image GimpySpetsnaz · Nov 15, 2013 at 12:02 PM 0
Share

Thanks Jesse. I too, was looking for a solution to this exact thing! Here is my current code responsible for the Z rotations:

 RaycastHit hitInfo;
             if (Physics.Raycast(transform.position, -Vector3.up, out hitInfo, 40.0f))
 {    Quaternion rot = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
             
     transform.rotation =
           Quaternion.Slerp (transform.rotation,rot, Time.time * centeringSpringForce);    
             
         }

Right now, the code will align my Z axis to the normal of the polygon underneath my ship, but it also freezes my ships ability to be rotated in the other axis. I've been scratching my head for a little while now, do you have any ideas for what might be causing my ships X and Y rotation to be frozen when raycast hit is firing, and how to make it just affect the Z?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

"Free" rotation about a sphere 0 Answers

Camera Rotation Question 1 Answer

Transform.Translate but ignore rotation on one axis 0 Answers

How can I align Axis? 0 Answers

Lock an axis from rotating 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