Download

Lock Camera In Place Temporarily

Hi all,

I am trying to implement a “free cam”, similar to Mount and Blade, where you can hold down a key to freely rotate the camera around your character without modifying the character rotation, and then release to snap back to the fixed third person camera. When the key is released, I want the fixed camera to snap back to the original rotation that was there before.

Currently, I am doing this by storing a “LookRotation” Rotator which is updated alongside the controller rotation (since the control rotation continues to be updated when moving the free cam around). It stops updating when the player is in free cam mode, and then when they release it sets the control rotation back to whatever LookRotation is.

This is fine for basic camera movement, but I am running into problems when the player is shooting while in free cam mode. The fixed camera continues to be updated along with the control rotation during free cam, so traces done using the fixed camera location and rotation will be disrupted by the use of free cam. The camera is on a spring arm, and any attempt to fix the spring arm in place while free cam is active doesn’t seem to work. Setting the rotation and location to absolute, disabling “use pawn control rotation” or setting the inherit pitch/yaw/roll flags to false causes the spring arm to snap back to a default location and rotation.

What I want is for the fixed camera to lock its relative location and rotation and stop updating so that if the player fires while using the free camera, they will continue to aim in the same direction as the fixed camera before they activated free cam. Does anyone know the most efficient way to do this?

Thanks!

If I am understanding correctly your problem occurs when the player shoots, and the camera snaps-back to the player.
First, you can detach the camera permanently and re-attach it when needed (component detach) I think. Secondly, the problem is most likely due to the fact that the camera’s location and rotation is being used as the firing mechanism’s coordinates, meaning, it dictates the bullet’s trajectory. Either way, you should at least make sure the weapon’s muzzle dictates the shooting direction, not the camera.

I don’t really have UE4 here nor have I been using it for shooters, I hope this information is still correct. Give it a try!

Thanks for the response!

Basically, the problem is that my weapon firing traces are done using the player’s control rotation. The weapons are all simple hitscan types, so reason I do this instead of using the weapon’s muzzle is that:

A) I don’t want the accuracy of the weapon to be based on animation, I just want the trace to always hit exactly where the crosshairs are pointing no matter what animations are playing
B) I don’t want the server to have to simulate the bone transforms. We plan on using a custom networking solution to house around 250-500 players, so server and network load must be at a minimum

Although I have deactivated the third person camera and activated the free cam (and therefore the screen is rendered from the free camera perspective), the controller rotation (and therefore the third person camera) is still being rotated by the player moving the mouse, which means that in the background the third person camera is still being moved around.

Let’s say the player aims at a bullseye in third person mode, and then switches to free cam. What I want is for the player to be able to swivel the free camera to look behind the character and when they shoot, the character still shoots at the bullseye. Right now, the traces will start being targeted behind them (i.e. following the free camera’s rotation) while the physical representation of the character looks as though they’re still aiming at the bullseye, which obviously looks weird.

I’ll keep experimenting and see if I can come up with a simple solution, but any suggestions would be much appreciated!

Ok second thought:

What I really need is for the free camera to be rotated independently of the controller rotation. Basically, when the player hits the free camera button, the control rotation stops being updated and the free camera is rotated using the mouse. When the player releases the free camera button, the controller starts being updated again.

Does anyone know how to do something like this? I’ll do my own research now but if anyone has implemented something similar then let me know!

Hi everyone,

In case anyone else is interested in the future, I managed to implement this feature simply by overriding the AddControllerPitchInput and AddControllerYawInput functions in the character class:


void ABaseCharacter::AddControllerPitchInput(float Val)
{
    if (bUseFreeCam)
    {
        APlayerController* PC = Cast<APlayerController>(GetController());
        FRotator newRotation = FreeCameraBoom->GetComponentRotation();

        newRotation.Pitch += Val * PC->InputPitchScale;

        FreeCameraBoom->SetWorldRotation(newRotation);
    }
    else {
        Super::AddControllerPitchInput(Val);
    }
}

void ABaseCharacter::AddControllerYawInput(float Val)
{
    if (bUseFreeCam)
    {
        APlayerController* PC = Cast<APlayerController>(GetController());
        FRotator newRotation = FreeCameraBoom->GetComponentRotation();

        newRotation.Yaw += Val * PC->InputYawScale;

        FreeCameraBoom->SetWorldRotation(newRotation);
    }
    else {
        Super::AddControllerYawInput(Val);
    }
}

Effectively, if the player is in free cam mode, it hijacks the mouse input so that the player controller never receives it. The player can swivel the camera around but as far as the server, clients and the character itself is concerned, the controller is still looking in whichever direction it was when free cam was activated.

One thing I find slightly confusing here though is that despite deltatime not being used in the calculations, the rotation is still independent of framerate. I was expecting low framerates to cause the camera to become less responsible, and high framerates to cause it to spin all over the place, but this is not the case. I noticed a similar thing with the way the Player Controller is rotated. Is the axis input already adjusted for delta time at the point of entry?