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 CaioRosisca · May 26, 2014 at 04:43 PM · camerarotateangle

How to know if rotating clockwise or counterclockwise

I've a pathfinding and at each step I need to figure out if I need to turn left or right, kind of a GPS like thing. At each step I rotate my camera Y axis towards the next node and move it's position towards it, working fine until now. Once I reach the next point I need to determine if I need to turn left or right, here's the problem, how can I know it? By the way, I couldn't find a good way to make the camera achieve the rotation I would like, here's the code right now, any help on how to make it better is appreciated. For the left/right prediction I tried calculating the angle between points, and relative to current camera angle using Mathf.atan2 but couldn'd get to an answer. In this image the camera just moved to the point marked as current position and is about to rorate from the green arrow to the the blue one, at this point I need to able to know that the camera is going to rorate to the right(clockwise). Any help? What I'm really trying to know is if the camera is going to rotate RIGHT or LEFT (and not how to rotate the camera, the rotation is working). alt text

 private void rotateCamera(string direction){
         float angle = 0;
         Quaternion temp = myCamera.transform.rotation;
         Vector3 direcao = Vector3.zero;
         if(direction == "r"){
             direcao = (new Vector3(nextNode.GetCoordinate().x,nextNode.GetCoordinate().y, myCamera.transform.position.z) - myCamera.transform.position).normalized;
         }else if(direction == "l"){
             direcao = (new Vector3(previousNode.GetCoordinate().x,previousNode.GetCoordinate().y, myCamera.transform.position.z) - myCamera.transform.position).normalized;
         }
         
         myCamera.transform.up = direcao;
         float desiredAngle = myCamera.transform.eulerAngles.z;
         myCamera.transform.rotation = temp;
         iTween.RotateTo(myCamera, iTween.Hash("z", desiredAngle, "time",timeToRotate, "islocal", true, "oncomplete", "moveCamera", "oncompletetarget", gameObject, "oncompleteparams", direction));
 }
help.png (16.5 kB)
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
0
Best Answer

Answer by CaioRosisca · May 26, 2014 at 06:37 PM

New function with solved problem, alot easier to solve than I would imagine. Thanks everyone for helping. Ignore non-english comments

 private void rotateCamera(string direction){
         //Salva a rotacao da camera
         Quaternion temp = myCamera.transform.rotation;
         
         //Direcao entre o ponto atual e o proximo
         Vector3 direcao = Vector3.zero;
         if(direction == "r"){
             direcao = (new Vector3(nextNode.GetCoordinate().x,nextNode.GetCoordinate().y, myCamera.transform.position.z) - myCamera.transform.position).normalized;
         }else if(direction == "l"){
             direcao = (new Vector3(previousNode.GetCoordinate().x,previousNode.GetCoordinate().y, myCamera.transform.position.z) - myCamera.transform.position).normalized;
         }
         
         //Guarda o angulo da camera pra poder calcular a diferenca dps
         float curAngle = myCamera.transform.eulerAngles.z;
         
         //Gira a camera pra salvar o novo angulo
         myCamera.transform.up = direcao;
         
         //Salva o novo angulo
         float desiredAngle = myCamera.transform.eulerAngles.z;
         
         //Calcular a diferenca entre o angulo novo e o antigo
         float angleDif = desiredAngle - curAngle;
         
         //Pra ter um resultado entre 0 e 360
         if(angleDif < 0)
             angleDif+=360;
         
         print ("AngleDif: "+angleDif);
         //Vira pra esquerda
         if(angleDif > 0 && angleDif < 180){
             print ("turning left");    
         //Vira pra direita
         }else{
             print ("turning right");    
         }
         
         //Volta a rotacao da camera pra rotacao normal
         myCamera.transform.rotation = temp;
         
         //Faz o tween pra nova posicao
         iTween.RotateTo(myCamera, iTween.Hash("z", desiredAngle, "time",timeToRotate, "islocal", true, "oncomplete", "moveCamera", "oncompletetarget", gameObject, "oncompleteparams", direction));
     }
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 meat5000 ♦ · May 26, 2014 at 08:15 PM 1
Share

Good job :)

  • the comments/answers that got you to your answer.

avatar image CaioRosisca · May 27, 2014 at 05:27 PM 0
Share

Just did, I didn't know I could +1 comments

avatar image MianAbdul · Jun 02, 2020 at 08:11 AM 0
Share

I have an object spinning on its axis how can I deter$$anonymous$$e that the spinning motion is "CW" or "CCW".

avatar image CaioRosisca · Jun 02, 2020 at 04:15 PM 0
Share

@mian_abdul float angleDif = $$anonymous$$athfUtils.Clamp0360(targetAngle- currentAngle); if(angleDif > 0 && angleDif < 180) { //ccw } else { //cw }

avatar image
1

Answer by Sprawl · May 26, 2014 at 04:52 PM

When reaching a node and calculating the next direction to go in, Get both your current Direction and the new direction. Substract the current to the next and if the result is between 0 and 180, you're going left. Otherwise you're going right.

If the number is negative, add 360 degrees to it.

Comment
Add comment · Show 1 · 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 CaioRosisca · May 26, 2014 at 05:05 PM 0
Share

@Sprawl I've eddited the post with the current code I'm using, both currentDirection and newDirection are Vector3, how would I go about substracting them and getting a result in degrees?

avatar image
1

Answer by robertbu · May 26, 2014 at 05:14 PM

One way to handle this is to punt on iTween, and use a routine that takes vectors instead of angles. Here is a bit of untested code:

 IEnumerator RotateTo(Vector3 dir, float speed) {
     Quaternion lookRot = Quaternion.LookRotation (dir, Vector3.forward);
     while (lookRot != transform.rotation) {
         transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRot, speed * Time.deltaTime);
         yield return null;
     }
 }

...and you would use it like:

  StartCoroutine(RotateTo(newDirection, 75.0f));
Comment
Add comment · Show 6 · 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 CaioRosisca · May 26, 2014 at 05:18 PM 0
Share

@robertbu Thanks, by what I understood that solves the problem involving the angles, but can it give me the answer for if I'm rotating right or left? Sorry for my bad english .. EDIT: just tried the routine and it doesn't work :(, I can still use iTween tho, no problem at all.

avatar image meat5000 ♦ · May 26, 2014 at 05:33 PM 1
Share

Cross product matey

avatar image CaioRosisca · May 26, 2014 at 05:43 PM 0
Share

@meat5000 I guess it's beyond my knowledge, if I'm right by crossing 2 vectors, for example, (1,0,0) and (0,1,0), that would result in (0,0,1), like multiplying 2 axis would result in the 3rd one..right? But I still don't know how to use it to find my right/left answer. Could you help me with some code? or at least pseudo code

avatar image robertbu · May 26, 2014 at 06:41 PM 2
Share

I just tested the coroutine, and it does work, so I'm unsure how you integrated it. You can do:

 Vector3 cross = Vector3.Cross(currentDirection, nextDirection);
 
 if (Vector3.Dot(cross, Vector3.forward) > 0.0) {
     Debug.Log("Right turn")
 }
 else {
     Debug.Log("Left turn");
 }
avatar image meat5000 ♦ · May 26, 2014 at 06:56 PM 1
Share

Yes Caio, crossing x and y, the direction the z flies out deter$$anonymous$$es direction.

http://forum.unity3d.com/threads/153618-Cross-Product

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

23 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

Related Questions

How to lock camera angle on key press? 1 Answer

Rotate player 360 degrees once 0 Answers

Rotate camera to character angle 0 Answers

3rd person unit control 0 Answers

Object rotates with camera 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