transform.rotate(Vector3)
rotates but it rotates. meaning it adds to the current facing direction.
But what if i want to RESTORE my initial facing direction of a character in some part of the game?
Like my character is facing right at the start of the game and then pressing
both W and D at the same time makes him run diagonal forward to the right instead of just to the right.
and i want that after the user lets go of those 2 keys, then the character should face the right side again.
Problem is, if i do transform.rotate( originalangle);
it’s not gonna be the original facing direction. it’s gonna add the original angle to the current and so
it’s not gonna end up looking facing right.
Here’s what i have so far and it didn’t work
float myOriginalRotationY;
void Start(){
myOriginalRotationY = transform.eulerAngles.y;
}
void Update () {
//check combination keys
//4 combinations
//W+ D, , W + A, , S + A, , S + D
//Here in this example I'm just starting with testing
//W+D combo
if( Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D)) { //forward diagonal right
if(currentDir !=7){
//Rotate Based on current facing direction
//-2 is when user pressed the S to face down
//or to run down. 1 is D for facing right
//-1 is A for facing left
if(currentDir == -2)
transform.Rotate ( 0, 135, 0, Space.Self);
if(currentDir == -1)
transform.Rotate ( 0, -135, 0, Space.Self);
if(currentDir == 1)
transform.Rotate ( 0, -45, 0, Space.Self);
currentDir = 7;
}
return;
}
//once done with the Press & Hold the above, return to //either look left or right
//depending on the keycombination executed
if( currentDir == 7){ //*******FORWARD DIAGONAL RIGHT
if( Input.GetKeyUp(KeyCode.W) && Input.GetKeyUp(KeyCode.D)){
Debug.Log( "user let go the Diagonal");
//Return to original Right direction
transform.Rotate ( transform.eulerAngles.x, myOriginalRotationY, transform.eulerAngles.z);
currentDir = 1;
}
return;
}
//Then there's more stuf in the update
//handdling if single keys W A D S were pressed and changing //chracter rotation for each
So anyways, my way of trying to return my character to the original facing direction didn’t work. How do i make it work?
By the way, the game i’m making, is 3D but not FPS.
Basically you have a camera at about the same level of the character but at a fixed distance from the character. The player can use the W S D A keys to move left or right or forward or towards the camera.
Then press key combinations liek W+ D to walk in the diagonal forward right, diagonal forward left, and so on.
Anyways, this question is about HOW TO RESTORE ORIGINAL FACING DIRECTION(either left or right).
How to store the original facing direction and restore it after my walking diagonal move.