- Home /
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?
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
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.
Answer by Noah-1 · May 08, 2012 at 05:12 AM
Then this should work:
function Update() {
transform.Rotate(0, 5, 0);
}
LOL - If you don't comprehend the question, why bother answering with garbage?
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.
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
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);
}
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.
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);
}
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
Follow this Question
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