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 nestor_d · Feb 05, 2015 at 12:41 PM · rotationanglewhilewhile-loop

How do I make something rotate until a condition is met

I want to make a game object rotate until its Z axis it's pointing towards a specific point. I have a variable named angle which is the angle between the object's Z axis and the position of the point which it needs to point to. I have the following code right now:

 while (angle > 0)
             {
                 transform.Rotate (Vector3.right * rotationSpeed * Time.deltaTime);
                 transform.Rotate (Vector3.up * rotationSpeed * Time.deltaTime);
             }

but it always ends up in an infinite loop and crashing because it just rotates by the amount I'm telling it to. So is there any way to tell the object to keep rotating until the angle is 0?

Comment
Add comment · Show 9
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 samqweqwe · Feb 05, 2015 at 07:14 PM 0
Share

Where do you set the value of angle, because it is not being increased or decreased in the loop. Could that be the cause of the issue?

avatar image nestor_d · Feb 05, 2015 at 07:28 PM 0
Share

I think I found that the problem is that the way the object rotates makes it impossible for the angle to ever close, so yeah, you're right about that. I still have no idea how to solve my problem, my intuition would be to define two angles: one equal to the angle between the z vector of the game object I'm trying to rotate and the transform.position.x of the target, and another angle but with the y component of the target, I don't know if I'm being clear enough... Anyway then I'd have to set up two while loops, one for each angle, that way each one will have to equal zero at some point and exit the loop, but I have no idea how to do it yet.

avatar image samqweqwe · Feb 05, 2015 at 07:36 PM 0
Share

Could you work out the movement before the loop, eg

 float xRot = targetAngleX - currentAngleX;
 float yRot = targetAngleY - currentAngleY;

and then work out the speed for each axis. After that you could then go into a loop and add the necessary speed to each one?

avatar image nestor_d · Feb 05, 2015 at 07:48 PM 0
Share

That sounds about right. However, and I hope this isn't much trouble, could you elaborate a little more, please? I'm new to unity and C# I'm not entirerely sure about some of the concepts. I'm guessing xRot and yRot would store a value for an angle wich I would then turn into a rotation by using

  transform.Rotate(Vector3.up * xRot);
  transform.Rotate(Vector3.right * yRot);
 

but, I'm not sure about how to retrieve the values of targetAngle and currentAngle. What I did in my original code to get the angle was this:

  float angle = Vector3.Angle(transform.forward, CameraOp.moveToDest)

In that context transform.forward was the angle of the thing I was trying to rotate and CameraOp.moveToDest a position value retrieved from a raycast hit. what I'm trying to do is to make the object point at CameraOp.moveToDest without using LookAt

avatar image samqweqwe · Feb 05, 2015 at 08:06 PM 0
Share

What data type is CameraOp and moveToDest?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Superrodan · Feb 05, 2015 at 09:45 PM

If the point you want to point towards is a rotation vector you can use RotateTowards, I believe.

 Quaternion myQuat = Quaternion.Euler(transform.localEulerAngles);
 Quaternion targetQuat = Quaternion.Euler(YOURTARGETVECTOR);
 
 while(myQuat!=targetQuat)
 {
 transform.localRotation = Quaternion.RotateTowards(myQuat, targetQuat, 10.0f);
 myQuat = Quaternion.Euler(transform.localEulerAngles)
 }

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 Superrodan · Feb 05, 2015 at 09:48 PM 0
Share

This is for local rotation. For world rotation remove the local so it's just EulerAngles I believe.

This is all untested and more of a guess based on how I would try to approach the problem.

avatar image nestor_d · Feb 05, 2015 at 09:49 PM 0
Share

thanks four your answer! but sadly no... the point is just a static Vector3. It's the transform.position of another game object, I haven't got into Quaternions yet

avatar image Superrodan · Feb 05, 2015 at 09:56 PM 0
Share

Try this in the above code, and it still might work:

 Quaternion targetQuat = Quaternion.LookRotation(YOURTARGETVECTOR);

avatar image Superrodan · Feb 05, 2015 at 10:12 PM 0
Share

Actually looking at the above comment thread going on it seems I might be close but not entirely there with my usage of LookRotation. I think this is closer:

 Quaternion myQuat = Quaternion.Euler(transform.EulerAngles);
 Vector3 targetToLookAt = target.position - transform.position;
 Quaternion targetQuat = Quaternion.LookRotation(targetToLookAt);
 
 while(myQuat!=targetQuat)
 {
 transform.rotation = Quaternion.RotateTowards(myQuat, targetQuat, 10.0f);
 myQuat = Quaternion.Euler(transform.EulerAngles)
 }

As they said above, putting this in a while loop is probably not the best way to do this. Having it in your Update script is better, but I wrote my answer based on the method you put in your initial question.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I call a function containing a while loop, inside the update function? 2 Answers

Angle to Rotation 2 Answers

0-360 Y degree from Vector3.Angle 1 Answer

Calculate rotation angle for two side of cube ( like dice ) from Quaternion.identity 0 Answers

Issue finding an angle with trigonometry 1 Answer


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