3rd Person Camera

Notes:

This isn't the tidest code. I have butchered it from the 'Metaball' TC.

It overrides the PlayerCalcView and PlayerMove functions to give a 3rd Person Camera.

This code should all be in your PlayerController class.
Its should be straight-forward to get it working, but youll obviously need to put these bits of code in the right places.
You may need to play with the joystick settings in your user.ini file

Please update, improve the code and share with the group.

GOOD LUCK!

// variables used by the camera code

var vector CurrentCameraLocation;
var rotator CurrentCameraRotation;
var Actor CurrentViewActor;

var vector CameraTransStartLocation;
var rotator CameraTransStartRotation;
var float CameraTransStartTime;
var float CameraTransEndTime;

var Actor CameraTargetActor;
var vector CameraTargetLocation;

var rotator MyViewRotation;
var vector CameraTargetOffset;
var vector ActualCameraLocation;

var float ViewDistance;
var float DesiredViewDistance;
var float MaxViewDistance;

var float CameraPitchMin;
var float CameraPitchMax;

function int AngleMod( int a )
{
    if (a >= 0)
        return a % 65536;
    else
        return (a % 65536) + 65536;
}

// needs to be called during startup to initiaise camera variables
function InitialiseCamera()
{
      MyViewRotation = Pawn.Rotation;

    ViewDistance = 256;
    DesiredViewDistance = 256;
    MaxViewDistance = 1024;

    CameraTransStartTime = level.TimeSeconds;
    CameraTransEndTime = level.TimeSeconds + 1.0f;
    CameraTransStartLocation = CurrentCameraLocation;

    CameraTargetActor = Pawn;
    CameraTargetOffset = vect(0,0,0);

    CameraTransStartRotation = CurrentCameraRotation;
    CameraTransStartRotation.Yaw = AngleMod(CameraTransStartRotation.Yaw);

    CameraPitchMin=-13000;
    CameraPitchMax=768;
}

// this overrides the existing PlayerCalcView
// this function calculates where the camera should be positioned in the world
function PlayerCalcView(out Actor ViewActor, out vector CameraLocation, out rotator CameraRotation)
{
    local vector newcamloc, hitloc, hitnorm, ttemp;
    local rotator newcamrot;
    local float transdelta;

    if ( LastPlayerCalcView == Level.TimeSeconds )
    {
        ViewActor        = CurrentViewActor;
        CameraLocation    = CurrentCameraLocation;
        CameraRotation    = CurrentCameraRotation;
        return;
    }

    LastPlayerCalcView = level.TimeSeconds;

    if ( (Abs(ViewDistance - DesiredViewDistance) > 0.05) &&
        (ViewDistance < MaxViewDistance) )
    {
        ViewDistance += (DesiredViewDistance - ViewDistance) * 0.1;
    }
    else
    {
        ViewDistance = DesiredViewDistance;
    }

    if (ViewDistance < 12) ViewDistance = 12;

    ViewDistance=256.000000;

    ViewActor = self;

    newcamloc = Pawn.Location - vector(MyViewRotation) * ViewDistance;
    newcamrot = rotator((Pawn.location + CameraTargetOffset) - newcamloc);

    if ( (newcamloc.Z < (ViewActor.Location.Z+50)) && (aLookUp==0) )
    {
        newcamloc.Z += (ViewActor.Location.Z+50-newcamloc.Z)*0.3;
    }

    newcamrot = rotator((Pawn.location + CameraTargetOffset) - newcamloc);

    ActualCameraLocation = newcamloc;
    CurrentViewActor = ViewActor;

    newcamrot.Yaw = AngleMod(newcamrot.Yaw);

    // move towards target
    if (CameraTransEndTime < level.TimeSeconds)
    {
        CameraLocation = newcamloc;
        CameraRotation = newcamrot;
    }
    else
    {
        transdelta = sin( (Pi/180.0)*90.0 * ( (level.TimeSeconds - CameraTransStartTime) / (CameraTransEndTime-CameraTransStartTime) ) );

        CameraLocation = CameraTransStartLocation + (newcamloc - CameraTransStartLocation) * transdelta;
        CameraRotation = newcamrot;
    }

    CurrentCameraRotation = CameraRotation;

    // Make sure the camera doesnt go outside walls
    if (Trace( hitloc, hitnorm, CameraLocation, Pawn.Location,false, vect(12,12,12) ) != none) //
    {
        CameraLocation = hitloc;
        ViewDistance = vsize(CameraLocation-Pawn.Location);
        MaxViewDistance = ViewDistance;
    }
    else
    {
        MaxViewDistance = 1024;
    }

    if ( (ViewDistance < 144) && (ViewDistance >= 32) )
    {
        CameraLocation.Z += 36 - ViewDistance*0.25;

        ttemp = Pawn.Location;
        ttemp.Z += 36 - ViewDistance*0.25;
        CameraRotation = rotator(ttemp-CameraLocation);
    }

    CurrentCameraLocation = CameraLocation;

    SetLocation( CurrentCameraLocation );
    SetRotation( CurrentCameraRotation );

    // if were really close to the player - then turn them off (could use transparency)

    if (Pawn != none)
    {
        if ( vsize(Pawn.Location-Location) <= 32 )
        {
            Pawn.bHidden = true;
        }
        else
        {
            Pawn.bHidden = false;
        }
    }
}

state PlayerWalking
{
    // this overrides the Playermove function in the PlayerWalking state
    function PlayerMove( float DeltaTime )
    {
        local vector X,Y,Z, NewAccel;
        local rotator OldRotation;
        local bool  bSaveJump;

        GetAxes(Rotation,X,Y,Z);

        // Update acceleration.
        NewAccel = aForward*X + aStrafe*Y;
        NewAccel.Z = 0;

        if ( VSize(NewAccel) < 1.0 )
            NewAccel = vect(0,0,0);
        else
            Pawn.SetRotation(rotator(NewAccel));

        GroundPitch = 0;

        MyViewRotation.Yaw += 24 * DeltaTime * aTurn;
        MyViewRotation.Pitch += 24 * DeltaTime * aLookUp;

        if (MyViewRotation.Pitch > CameraPitchMax)
            MyViewRotation.Pitch = CameraPitchMax;

        if (MyViewRotation.Pitch < CameraPitchMin)
            MyViewRotation.Pitch = CameraPitchMin;

        if ( bPressedJump && Pawn.CannotJumpNow() )
        {
            bSaveJump = true;
            bPressedJump = false;
        }
        else
        {
            bSaveJump = false;            
        }    

        ProcessMove(DeltaTime, NewAccel, DCLICK_None, OldRotation - Rotation);
        bPressedJump = bSaveJump;        
    }
}

defaultproperties
{
    CameraPitchMin=-16300.000000
    CameraPitchMax=768.000000
    ViewDistance=256.000000
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License