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 Griffo · Mar 20, 2013 at 12:43 PM · quaternionfunctionslerpwhile

Convert function into a While loop

Hi,

I have this function that rotates the top half of the Mech back facing forward to match the bottom half after an attack -

 function rotateTurret(){
 
     var lookPos = transform.forward;
     var rotation = Quaternion.LookRotation(lookPos);
 
     rotation *= Quaternion.FromToRotation(Vector3.up, Vector3.right);
     rotation *= Quaternion.Euler(XattackDegrees, YattackDegrees, 0);
     mechTurret.rotation = Quaternion.Slerp(mechTurret.rotation, rotation, Time.deltaTime * 2);
 }

What I want to do is covert it into a while function, I thought I could add something like -

 while(mechTurret.rotation != transform.forward){

But Unity wont allow that, or -

 while(mechTurret.rotation != transform.rotation){

But Unity wont allow that ether, can someone point me in the right direction please, thanks.

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 Graham-Dunnett ♦♦ · Mar 20, 2013 at 01:05 PM 0
Share

transform.forward is a Vector3 and mechTurret.rotation is a Quaternion. You cannot compare these against each other.

avatar image Griffo · Mar 20, 2013 at 01:08 PM 0
Share

What about -

 while(mechTurret.rotation != transform.rotation){

1 Reply

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

Answer by whydoidoit · Mar 20, 2013 at 01:28 PM

Firstly checking for an equality will likely never work (or take far longer than you expect).

Also your syntax for the condition is trying to do a !mechTurret.rotation - which makes no sense :)

I think you meant:

      while(mechTurret.rotation != transform.rotation) //Still not a good idea

Or

      while(!(mechTurret.rotation == transform.rotation))

You can try:

      while((mechTurret.eulerAngles - transform.eulerAngles).sqrMagnitude > 0.1f)
Comment
Add comment · Show 7 · 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 Griffo · Mar 20, 2013 at 02:03 PM 0
Share

whydoidoit, thanks for that, sorry about the syntax error I've corrected it now, I've tried you suggestion but it won't yield just keeps on going, I've tried raising the value but still wont yield.

avatar image whydoidoit · Mar 20, 2013 at 02:57 PM 0
Share

You've used StartCoroutine to start it?

and have a yield in the body?

avatar image Griffo · Mar 20, 2013 at 03:14 PM 0
Share

I thought in UnityScript, StartCoroutine is not necessary as the compiler does for you, for testing reasons I'm calling it from the Start function so there is no confusion of it being called again, and yes it has a yield.

avatar image Griffo · Mar 20, 2013 at 03:53 PM 0
Share

I've just made a test scene with 2 cubes, top one for the turret offset at 90 degrees, and a very basic script with this code in and calling it from the start function, and I'm glad to say it's working, so it's something in my AI script attached to the $$anonymous$$ech that's causing the problem, so thanks for the help, much appreciated.

avatar image Griffo · Mar 22, 2013 at 05:32 PM 0
Share

Ok I now have the problem that the original models axis are incorrect so have to compensate when tracking the target with -

 rotation *= Quaternion.FromToRotation(Vector3.up, Vector3.right);

So when I use this script to return the turret back to match the lower half with the above line in, it never yields, without the line in it rotates and yields but with the $$anonymous$$echs head out of orientation -

 function rotateTurret(){
 
     while((mechTurret.eulerAngles - transform.eulerAngles).sqr$$anonymous$$agnitude > 0.01f){
     
 // Rotate the $$anonymous$$ech Turret to face the direction of the bottom half of the $$anonymous$$ech
         var lookPos = transform.forward;
         var rotation = Quaternion.LookRotation(lookPos);
 
         rotation *= Quaternion.FromToRotation(Vector3.up, Vector3.right);
 
         mechTurret.rotation = Quaternion.Slerp(mechTurret.rotation, rotation, Time.deltaTime * 1);
         yield;
     }
 }

So what I'd like to know is how would I rewrite the while line to compensate for the axis correction of Vector3.up and Vector3.right ?

The models axis cannot be corrected as I purchased it off the net and it comes with lots of animations, I've been talking to the makers, they have been responsive and helpful, offered me a refund but I'd like to keep and use it if possible.

Thanks.

Show more comments

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

12 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

Related Questions

Coroutine While loop help 2 Answers

Extrapolating a new rotation from two existing rotations. 2 Answers

Quaternion Rotation Smooth 1 Answer

Call a function repeatedly while a boolean is true 3 Answers

Run a function aslong as a button is pressed 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