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
3
Question by AGP · Dec 21, 2010 at 08:48 PM · rotationquaternionlookat

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.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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);
}

}

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
1

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.

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 AGP · Dec 22, 2010 at 02:33 PM 0
Share

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.

avatar image Jesse Anders · Dec 22, 2010 at 03:59 PM 0
Share

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).

avatar image AGP · Dec 22, 2010 at 10:20 PM 0
Share

It's been edited into my first post (wouldn't fit as a comment for some reason).

avatar image Jesse Anders · Dec 22, 2010 at 11:38 PM 0
Share

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.)

avatar image
0

Answer by AGP · Dec 23, 2010 at 12:55 AM

Thanks for the hint. Here it is.

Edit: FINALLY INDENTED IN FOLLOWING POST.

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

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

No one has followed this question yet.

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


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