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 JSorrentino · May 08, 2012 at 04:21 AM · rotationquaternion

Rotating an object in world space.

Here's a very simple code snippet, for use in rotating a camera SkyBox. This is the simplest code I can think of to demonstrate what I am trying to do, as apparently I'm terrible with this math...

 function Start()
 {
     //transform.localRotation = Random.rotation;
 }
 
 function Update()
 {
     transform.Rotate(Vector3.right, 10.0f * Time.deltaTime, Space.World);
 }

This simple script works perfectly, until the commented line is un-commented in the Start() function.

The issue is that the Start function is randomly rotating the object and changing it's local rotation, obviously. The update function is not properly rotating the object in world space because the objects local rotation is being rotated on the wrong axes.

It looks like i need Quaternion math to rotate properly when the local rotation is randomized. Can someone help me with the math to properly modify the rotation as desired?

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 Noah-1 · May 08, 2012 at 05:08 AM 1
Share

Please provide more info on what youre trying to achieve, do you only want to rotate a camera ? If that is the case, check out Unity Script Reference and go to Common Operations

avatar image JSorrentino · May 08, 2012 at 05:09 AM 0
Share

The post states exactly what I'm trying to achieve.

3 Replies

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

Answer by JSorrentino · May 08, 2012 at 08:44 PM

OK, well I found the issue and saved my sanity... My method above was fine, rotations were happening as I wished but there was another influence still active and also playing with the localRotation of the objects.

This was an error on my side, everything actually works as it should. I appreciate everyone's help and apologize for wasting your time on this.

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 Noah-1 · May 08, 2012 at 05:12 AM

Then this should work:

 function Update() {
     transform.Rotate(0, 5, 0);
 }
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 JSorrentino · May 08, 2012 at 05:15 AM 0
Share

LOL - If you don't comprehend the question, why bother answering with garbage?

avatar image Noah-1 · May 08, 2012 at 05:25 AM 2
Share

Dude calm down, Im just trying to help... tell me for how long have you been working with Unity? People here really appreciate well explained questions and get pissed when someone shows ego. So good Luck with your question.

avatar image
0

Answer by AlucardJay · May 08, 2012 at 06:12 AM

EDIT : deals with the world-space vs local-space issue :

looking back to the issue of world space vs local space, you probably need to change the Vector3 (world space) to a transform (local space) in the Update :

 function Start() 
 {
     transform.localRotation = Random.rotation;
 }
 
 function Update() 
 {
     transform.Rotate(transform.right, 10 * Time.deltaTime, Space.World);
 }

. first answer just with Random.rotation workaround :

 #pragma strict
 
 function Start() 
 {
     //var rotX = Random.Range(0, 360);
     //var rotY = Random.Range(0, 360);
     //var rotZ = Random.Range(0, 360);
     //transform.localRotation = Quaternion.Euler(rotX, rotY, rotZ);    
     // or just simplify with : 
     transform.localRotation = Quaternion.Euler(Random.Range(0, 360), Random.Range(0, 360), Random.Range(0, 360));
 }
 
 function Update() 
 {
     transform.Rotate(Vector3.right, 10 * Time.deltaTime, Space.World);
 }

http://unity3d.com/support/documentation/ScriptReference/Quaternion.Euler.html

Comment
Add comment · Show 4 · 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 AlucardJay · May 08, 2012 at 06:27 AM 0
Share

I am not familiar with nor used Random.rotation , but the Unity Scripting Reference says it returns a Quaternion, which leaves the question - why doesn't it work. I posted this answer to help show Random and Quaternion.Euler

further reading and testing shows that Random.rotation actually does work and return a Quaternion , so actually I don't know why you had this problem (i.e. this script is tested and working) :

 #pragma strict

 function Start() 
 {
     //transform.localRotation = Quaternion.Euler(Random.Range(0, 360), Random.Range(0, 360), Random.Range(0, 360));        
     Debug.Log(Random.rotation);        
     transform.localRotation = Random.rotation;
 }

 function Update() 
 {
     transform.Rotate(Vector3.right, 10 * Time.deltaTime, Space.World);
 }
avatar image JSorrentino · May 08, 2012 at 06:30 AM 0
Share

Actually, no, it's not working. I tested it but it still rotates the object it's attached to in a skewed way, based off the random localRotation assigned in Start().

I think, but I'm not 100% sure on this, that i need to re-calculate a proper Vector3.right based off the local rotation of the object so that it rotates properly in the world. If I don't do this, the rotation of the "SkyBox" is totally random and not the desired result.

I apologize if I'm not explaining this properly.

avatar image AlucardJay · May 08, 2012 at 06:31 AM 0
Share

looking back to the issue of world space vs local space, you probably need to change the Vector3 (world space) to a transform (local space) in the Update : (this should have been my first answer)

 function Start() 
 {
     transform.localRotation = Random.rotation;
 }
 
 function Update() 
 {
     transform.Rotate(transform.right, 10 * Time.deltaTime, Space.World);
 }
avatar image AlucardJay · May 08, 2012 at 06:52 AM 0
Share

does changing the method from Vector3 to transform work? I got a little confused with the question and title :

"The update function is not properly rotating the object in world space because the objects local rotation is being rotated on the wrong axes." .

Vector3 IS properly rotating the object in WorldSpace , you want to rotate on one of the Local object's axis. (transform)

by the same argument , you can set your Random.rotation in WorldSpace , the update still needs to apply to the object's local rotation

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Look at like rotating around y axis 1 Answer

ConfigurableJoint - angular positions (rotation) problem 1 Answer

Make Quaternion affected by float 1 Answer

Rotation of an object with quaternion 1 Answer

Reverse rotations problems... 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