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 Drathro · Nov 07, 2011 at 08:52 AM · quaternion

how to make a simple full rotational quaternion for cameras

Hello, I am quite new to Unity, and I was wondering how to (in C# preferably) accomplish a simple full 360 degree in all directions rotation through quaternions (space flight simulation style)? Nothing fancy, no limitations, just being able to rotate in all directions, WITHOUT having the "input direction" change, meaning once I've passed 180, I don't want the left and right to swap. So rotation relative to the world is what I'm looking for I THINK... Help is greatly appreciated! Thanks!

EDIT: To be more specific, I need full pitch roll and yaw on ALL 3 axis, the rotations will be added via mouse movement/joystick movement, roll will occur in place of yaw when a button is pressed, and become yaw again when the button is released.

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 syclamoth · Nov 07, 2011 at 09:11 AM 0
Share

Could you go into a bit more detail here? It's really not all that hard, but there's a few things you might be talking about here. For example, under what circumstances would the 'input direction change'? I don't quite follow what you're talking about there. That doesn't seem like something that would happen naturally.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Nov 07, 2011 at 12:37 PM

If rotating only around axis Y (horizontal plane) and X (vertical plane), you can detect when the object is upside down and invert the horizontal control direction. If you plan to rotate around Z too, however, the resultant movement may become too weird to understand - it's better to let Z alone, or use it just to bank in small angles.

void Update(){
    // define the horizontal rotation direction:
    float dir = 1;
    if (transform.up.y < 0) dir = -1; // if upside down, invert horizontal dir
    float dt = turnSpeed * Time.deltaTime;
    // turn right/left (around Y) according to dir and Mouse X
    angles.y = (angles.y + dir * Input.GetAxis("Mouse X") * dt) % 360;
    // turn up/down according to Mouse Y
    angles.x = (angles.x - Input.GetAxis("Mouse Y") * dt) % 360;
    // assign new angles to eulerAngles (or localEulerAngles)
    transform.eulerAngles = angles;
}
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
avatar image
0

Answer by cj_coimbra · Nov 07, 2011 at 12:42 PM

I don´t know if I understood your question correctly but...

You can use this to inform the angles in the 0 - 360 interval.

 transform.rotation = Quaternion.Euler(x, y, z);

If you want it to rotate over time, just store your desired rotation into a new Quaternion and then do the RotateTowards(...)

 Quaternion desiredRotation = Quaternion.Euler(0,180,0);
 transform.rotation = Quaternion.RotateTowards(transform.rotation, desiredRotation, rotationSpeed * Time.deltaTime);

Does it makes any sense?

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
avatar image
0

Answer by Drathro · Nov 07, 2011 at 06:24 PM

These look good, but I need rotation on ALL 3 axis. It's for a space-flight sim. type project, so turning is ALWAYS relative to the player. Thanks for the help so far!

Comment
Add comment · Show 1 · 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 aldonaletto · Nov 08, 2011 at 02:49 AM 0
Share

If you want local rotations, use Rotate:

float sensitivity = 60f;

void Update(){ float dt = sensitivity Time.deltaTime; float dx = Input.GetAxis("$$anonymous$$ouse Y") dt; float dy = -Input.GetAxis("$$anonymous$$ouse X") dt; float dz = Input.GetAxis("Horizontal") dt; transform.Rotate(dx, dy, dz); } This works fine with a first person camera (childed to the ship). With a 3rd person camera, the controls may seem too complicated, and the axis inversion trick here will only make things worse.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Add rotation to rigidbody AddForce? 3 Answers

with quaternion how rotate around global Y ? 2 Answers

Off Screen direction indicator HUD element / wrestling with Quaternion 1 Answer

Stage selection problem 1 Answer

How do I use this current Quaternion to make GameObject Joints move Correctly? 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