- Home /
How would i track rotational direction from my rotation cordinates?
So I am trying to figure out the best way to track whether my camera is moving left, right, left diagonal down, left diagonal up, right diagonal down, right diagonal up, up, and down so I can animate my sword to the correct attack the direction asks for. Currently my code looks like this -
if(checkXPos > currentPos.x + moveBoundry && checkYPos > currentPos.y + moveBoundry)
{
//Left Diagonal Downwards
}
else if(checkXPos > currentPos.x + moveBoundry && checkYPos < currentPos.y - moveBoundry)
{
//Left Diagonal Upwards
}
else if(checkXPos < currentPos.x - moveBoundry && checkYPos > currentPos.y + moveBoundry)
{
//Right Diagonal Downwards
}
else if(checkXPos < currentPos.x - moveBoundry && checkYPos < currentPos.y - moveBoundry)
{
//Right Diagonal Upwards
}
else if(checkXPos > currentPos.x && (checkYPos > currentPos.y + moveBoundry || checkYPos < currentPos.y - moveBoundry))
{
slashLeft = true;
}
else if(checkXPos < currentPos.x && (checkYPos > currentPos.y + moveBoundry || checkYPos < currentPos.y - moveBoundry))
{
slashRight = true;
}
else if(checkYPos > currentPos.y && (checkXPos > currentPos.x + moveBoundry || checkXPos < currentPos.x - moveBoundry))
{
//Down Slash
}
else if(checkYPos < currentPos.y && (checkXPos > currentPos.x + moveBoundry || checkXPos < currentPos.x - moveBoundry))
{
//Up Slash
}
else
{
CancelAnim();
}
So it already tracks my movement distance and my coordinate point that I'm looking at from one point to the another to make a line and I have check points to tell whether I am moving left or right, up or down from the difference of the two updating coordinates. How should I go at this and if there is a better method please by all means let me know thank you.
by 'camera moving' do you mean 'camera rotating'? $$anonymous$$y thinking is, if your camera is rotating (or moving in fact) that is usually controlled by something the users has already done, like moving the mouse up/down/diagonally, or pressing some combination of keys, so it would make more sense to calculate the motion performed from the source input. Rather than getting the input, using it to move something, then measuring how it moved in order to perform some action, you can skip the middle two steps.
Yeah I'm recording the cameras rotation x and y values with three different variables. One that records from a start position, one that records where my rotation is currently at, and one that updates to my current rotation after checking the difference of current rotation and the check rotation.
Answer by ben-rasooli · Apr 25, 2016 at 02:50 PM
No a better method, but you can clean up your code a bit, it's too confusing! You can nest your conditions to remove some duplication. For example:
bool movingLeft = checkXPos > currentPos.x + moveBoundry;
bool movingDown = checkYPos > currentPos.y + moveBoundry;
bool movingUp = checkYPos < currentPos.y - moveBoundry;
bool movingRight = checkXPos < currentPos.x - moveBoundry;
if(movingLeft){
if (movingDown) {
//Left Diagonal Downwards
} else if (movingUp) {
//Left Diagonal Upwards
} else {
//Left
}
} else if (movingRight) {
if (movingDown) {
//Right Diagonal Downwards
} else if (movingUp) {
//Right Diagonal Upwards
} else {
//Right
}
} else if (movingDown){
//Down
} else if (movingUp){
//Up
}
Your answer

Follow this Question
Related Questions
Float value = gameobject.rotation 2 Answers
How can I make player rotation control camera's x rotation and mouse control it's y rotation? 1 Answer
Getting the rotation of raycast target object and applying to the player (noob question) 1 Answer
Align a vector3 with parent's up-vector. 2 Answers
angular velocity only on one axis 1 Answer