Home > Software design >  How do i rotate the camera 180 Degrees to look back when a player holds down the space bar?
How do i rotate the camera 180 Degrees to look back when a player holds down the space bar?

Time:02-17

Im creating a game in unity where the player is able to move around a map using the arrow keys and changes direction using the mouse, But i am trying to get the camera to look back when the player holds down the space bar but keep moving forwards.

So Have this bit of code to rotate the camera 180 degrees, which is does but if i hold down the space bar if flashed as if it is switching between facing forwards and facing backwards. How do i stop this from happening?

private void Update()
{
    if (Input.GetKey(KeyCode.Space) && !globalScript.lookBack)
    {
        globalScript.lookBack = true;
        transform.Rotate(new Vector3(14, 180, 0));
    }
    else { 
        globalScript.lookBack = false;
    }
}

I am also trying to make the player always move forwards even when the camera is looking backwards (I have set my player to move in the direction that the camera is facing) so In other words when the player looks back they are still moving in the same direction that they were moving in when the camera is facing forwards.

I have come up with this to switch between moving forward and -forwards depending on the camera which i think is working but i don't think the lookBack is being accessed correctly and i'm not sure why.

Vector3 moveDir; 

if (globalScript.lookBack)
{
    moveDir = Quaternion.Euler(0f, targetAngle, 0f) * -Vector3.forward;
}
else
{
    moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
}

 controller.Move(moveDir.normalized * playerMoveSpeed * Time.deltaTime);

How do i go about trying to fix this so that i can still move forward when facing backwards?

CodePudding user response:

Your second block fires almost every frame, including any frame space isn't held down as well as any frame where globalScript.lookBack is true. You need to ensure that it only fires when both lookBack is true AND space isn't held down.

There also appears to be a problem where your rotation doesn't get set back to where it was before. If you change to using Quaternions this becomes easy. You can use Quaternion.Inverse to reverse the rotation you did to turn around, then apply that to the rotation

Altogether:

private void Update()
{
    Quaternion halfRot = Quaternion.Euler(14, 180, 0);
    if (Input.GetKey(KeyCode.Space))
    {
        if (!globalScript.lookBack)
        {
            globalScript.lookBack = true;
            transform.rotation *= halfRot;
        }
    }
    else if (globalScript.lookBack) { 
        globalScript.lookBack = false;
        transform.rotation *= Quaternion.Inverse(halfRot));
    }
}

Or, with a more "balanced" formatting:

private void Update()
{
    Quaternion halfRot = Quaternion.Euler(14, 180, 0);
    if (Input.GetKey(KeyCode.Space))
    {
        if (!globalScript.lookBack)
        {
            globalScript.lookBack = true;
            transform.rotation *= halfRot;
        }
    }
    else 
    {
        if (globalScript.lookBack) { 
        {
            globalScript.lookBack = false;
            transform.rotation *= Quaternion.Inverse(halfRot));
        }
    }
}
  • Related