- Home /
Copying the rotation matrix of an object.
I would like to make a copy of the rotation matrix of an object so that I could call GameObject.Transform.LookAt() so it will look at me (this part works), then restore its rotation back to where it had been before the LookAt call. I've tried copying gameObject.transform.localEulerAngles and copying the Transform.rotation quaternion (well, I could only copy the x, y, z and w parameters anyway). Neither worked. I also tried copying the original up and forward properties and that didn't work either. In all cases, the character ends up misaligned to the world (actually, come to remember, in this last case the character was aligned but no longer facing forward). How could I do that? Thanks in advance.
EDIT: The code is simple: function Update () { if (calculateDistance(GameObject.Find("Main Camera"), this.gameObject) > 4f) { if (rotationQuat.y != 10001) { //this.transform.LookAt(originalForward, originalUp); //this.gameObject.transform.rotation = rotationQuat; //this.gameObject.transform.localEulerAngles = rotation; this.gameObject.transform.rotation = rotationQuat; //rotation.y = 10001; rotationQuat.y = 10001; } this.animation.Play ("Walks"); //this.gameObject.transform.Translate(0, 0, .02f); } else { this.animation.CrossFade ("Attacks"); //rotationQuat = new Quaternion(this.gameObject.transform.rotation.x, this.gameObject.transform.rotation.y, this.gameObject.transform.rotation.z, this.gameObject.transform.rotation.w); //rotationQuat.SetLookRotation(this.gameObject.transform.rotation.GetLookRotation()); rotationQuat = this.gameObject.transform.rotation; //rotation = this.gameObject.transform.localEulerAngles; this.transform.LookAt(GameObject.Find("Main Camera").transform); } }
I appreciate your help.
Answer by AGP · Dec 27, 2010 at 06:49 PM
Does no one have a suggestion? Please?
function Update () {
if (calculateDistance(GameObject.Find("Main Camera"), this.gameObject) > 4f) {
if (rotationQuat.y != 10001) {
//this.transform.LookAt(originalForward, originalUp);
//this.gameObject.transform.rotation = rotationQuat;
//this.gameObject.transform.localEulerAngles = rotation;
this.gameObject.transform.rotation = rotationQuat;
//rotation.y = 10001;
rotationQuat.y = 10001;
}
this.animation.Play ("Walks");
//this.gameObject.transform.Translate(0, 0, .02f);
}
else {
this.animation.CrossFade ("Attacks");
//rotationQuat = new Quaternion(this.gameObject.transform.rotation.x, this.gameObject.transform.rotation.y, this.gameObject.transform.rotation.z, this.gameObject.transform.rotation.w);
//rotationQuat.SetLookRotation(this.gameObject.transform.rotation.GetLookRotation());
rotationQuat = this.gameObject.transform.rotation;
//rotation = this.gameObject.transform.localEulerAngles;
this.transform.LookAt(GameObject.Find("Main Camera").transform);
}
}
Answer by Jesse Anders · Dec 21, 2010 at 10:15 PM
You can store the rotation of an object by making a copy of Transform.rotation (for a top-level object) or Transform.localRotation (for a child object). Assigning this value back to the appropriate property/field of Transform will set the rotation of the game object back to what it was when you stored the rotation originally. (The same thing can be done with Transform.eulerAngles and Transform.localEulerAngles.)
If this isn't working for you, there must be something else wrong.
Well, I've tried both creating a new Quaternion and copying the primitive data types x, y, z, w and simply holding (or copying depending on how Javascript/$$anonymous$$ono works) the Quaternion in a variable (rotation = gameObject.transform.rotation). Then I assign the Quaternion back (gameObject.transform.rotation = rotation) but it doesn't go back to its original rotation.
As I said, there must be something else wrong. (Also, you can write 'transform.rotation' ins$$anonymous$$d of 'gameObject.transform.rotation'.) If you're still having trouble with this, maybe you could edit your original post and show us some of your code (or start a thread in the forums).
It's been edited into my first post (wouldn't fit as a comment for some reason).
Lol...did you look at your post after you made the edit? :-) (Hint: Use the little 'code' formatting button with the 1s and 0s on it when posting code.)
Answer by AGP · Dec 23, 2010 at 12:55 AM
Thanks for the hint. Here it is.
Edit: FINALLY INDENTED IN FOLLOWING POST.
Your answer
Follow this Question
Related Questions
Rotating object to face target, while being rotated to match terrain slope 1 Answer
LookAt on only 1 axis for 2D games? 1 Answer
Look at rotation at a moving object while moving (C#)(2D) 3 Answers
[Please!] Noob need's help with planet gravity movement. 2 Answers
Quaternion Equivalent of restricting an axes rotation to zero 3 Answers