Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 homer_3 · Sep 20, 2015 at 07:42 PM · scripting problem

Rotating objects in local space?

I'm trying to make this script that rotates and object back and forth but the object that is being rotated is a child of the object the script is on. While the script does rotate the child object back and forth, its facing is always wrong. I've tried a lot of different combinations of transforming the target rotation from one coord system to another at various stages of the calculation, but it never ends up correct. My script is below, can anyone see what's wrong with it?

     public float openSpeed;
     public float shakeSpeed; //how fast to shake
     public float openDist;
     public int shakeCount; //number of times to shake
     public float shakeRot; //how many degrees the object will shake
 
 
     Transform lid;
     int curShakeCount;
     int shakeDir;
     Vector3 startRot;
     Vector3 tarRot;
 
     // Use this for initialization
     void Start () {
         shakeDir = 1;
         curShakeCount = 0;
         Renderer renderer = gameObject.GetComponent<Renderer>();
         lid = gameObject.transform.GetChild(0);
         startRot = lid.TransformVector(lid.eulerAngles); //transform rotation from local to world space
         tarRot = Quaternion.AngleAxis(shakeRot, Vector3.forward) * startRot;
         tarRot = lid.InverseTransformVector(tarRot); //transform tar rotation from world to local space
     }
     
     // Update is called once per frame
     void Update () {
         if(curShakeCount <= shakeCount)
         {
             lid.rotation = Quaternion.LookRotation(Vector3.RotateTowards(lid.eulerAngles, tarRot, Time.deltaTime * shakeSpeed, 0));
             if(Mathf.Abs(Vector3.Distance(lid.eulerAngles, tarRot)) < .1f)
             {
                 shakeRot = -shakeRot;
                 tarRot = Quaternion.AngleAxis(shakeRot, Vector3.forward) * startRot;
                 tarRot = lid.InverseTransformVector(tarRot);
             }
         }
     }
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 homer_3 · Sep 20, 2015 at 09:16 PM 0
Share

I ended up going with a different approach to the solution by not making the object I'm rotating a child. Would be nice to know how to do it with a child object, but the below works in the mean time.

     public float openSpeed;
     public float shakeSpeed; //how fast to shake
     public float openDist;
     public int shakeCount; //number of times to shake
     public float shakeRot; //how many degrees the object will shake
     public float destThreshold;
 
     Transform lid;
     int curShakeCount;
     int shakeDir;
     Vector3 startRot;
     Quaternion tarRot;
 
     // Use this for initialization
     void Start () {
         shakeDir = 1;
         curShakeCount = 0;
         Renderer renderer = gameObject.GetComponent<Renderer>();
         lid = gameObject.transform.GetChild(0);
         lid.parent = null;
         startRot = lid.eulerAngles;
         lid.Rotate(Vector3.up, shakeRot);
         tarRot = lid.rotation;
         lid.rotation = Quaternion.Euler(startRot);
     }
     
     // Update is called once per frame
     void Update () {
         if(curShakeCount <= shakeCount)
         {
             //lid.rotation = Quaternion.Euler(Vector3.Lerp(lid.eulerAngles, tarRot, Time.deltaTime * shakeSpeed));
             lid.rotation = Quaternion.RotateTowards(lid.rotation, tarRot, Time.deltaTime * shakeSpeed);
             if($$anonymous$$athf.Abs(Quaternion.Angle(lid.rotation, tarRot)) < destThreshold)
             {
                 Quaternion temp = lid.rotation;
                 shakeRot = -shakeRot;
                 lid.Rotate(Vector3.up, 2*shakeRot);
                 tarRot = lid.rotation;
                 lid.rotation = temp;
             }
         }
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by tanoshimi · Sep 20, 2015 at 07:59 PM

Sounds like on line 29 you want to be setting the transform's localRotation, not its rotation...

http://docs.unity3d.com/ScriptReference/Transform-localRotation.html

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

30 People are following this question.

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

Related Questions

scripting error 1 Answer

C# Children of GameObject has it's script disabled after SetActive(false) and SetActive(true) 1 Answer

How can I create a random keycode generation ? 0 Answers

How to create a score manager script involving awarding points from multiple objects? 2 Answers

Script that works, throws in errors 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