- Home /
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).
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));
}
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));
}
Good job :)
the comments/answers that got you to your answer.
I have an object spinning on its axis how can I deter$$anonymous$$e that the spinning motion is "CW" or "CCW".
@mian_abdul float angleDif = $$anonymous$$athfUtils.Clamp0360(targetAngle- currentAngle); if(angleDif > 0 && angleDif < 180) { //ccw } else { //cw }
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.
@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?
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));
@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.
@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
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");
}
Yes Caio, crossing x and y, the direction the z flies out deter$$anonymous$$es direction.
Your answer
Follow this Question
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