Turn camera around without changing player movement

I am trying to have it so when the player hits Q, the camera spins around 180 degrees. I don’t want this to affect movement. Has anyone done something like this? How did you do so?

Thanks.

My script is super simple but does not have the camera rotation element. I don’t even know where to begin.

Movement

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;

private var moveDirection : Vector3 = Vector3.zero;

function Update() {
    var controller : CharacterController = GetComponent(CharacterController);
    if (controller.isGrounded) {
        // We are grounded, so recalculate
        // move direction directly from axes
        moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;
        
        if (Input.GetButton ("Jump")) {
            moveDirection.y = jumpSpeed;
        }
    }

    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;
    
    // Move the controller
    controller.Move(moveDirection * Time.deltaTime);

The default mouse script is used as well. Ideally, pressing Q would lock all rotation and just rotate the camera until it was 180 degrees from the forward direction still maintaining normal controls and not changing the forward vector.

Easiest would be to set the camera as a child to the character, and when you press Q, you inverse the forward vector of the camera (script attached to the camera):

if(Input.GetKeyDown(KeyCode.Q) == true)
{
    Vector3 Base = this.transform.parent.forward;
    Vector3 Inverted;
    Inverted.x = Base.x * -1.0f;
    Inverted.y = Base.y * -1.0f;
    Inverted.z = Base.z * -1.0f;
    this.transform.forward = Inverted;
}

And when you release the Q button, set the camera to look at its parent again:

if(Input.GetKeyUp(KeyCode.Q) == true)
{
   this.transform.LookAt(this.transform.parent);
}

The way to do this is:

  • Make the camera a child of the object being followed

  • Use RotateAround to change the position and rotation of your camera

    if(Input.GetKeyDown(KeyCode.Q))
    {
    transform.RotateAround(transform.parent.position,Vector3.up, 180f);
    }