UnrealScript Game Programming Cookbook

(Chris Devlin) #1
Chapter 8

227

We start by defining our local variables. We set MyPawnOwner to be the pawn that the
HUD is currently drawn for. Our weapon, W, is our pawn's currently equipped weapon.
If we don't have a pawn for whatever reason, get out of the function. There is no point
in wasting precious CPU cycles if there is no need for the function to get called.


  1. The next step involves our trace. We need to set the values for our trace,
    then perform one.
    /* If we have a weapon... /
    if ( W != None)
    {
    /* Values for the trace /
    StartTrace = W.InstantFireStartTrace();
    EndTrace = StartTrace + W.MaxRange() *
    vector(PlayerOwner.Rotation);
    HitActor = MyPawnOwner.Trace(HitLocation, HitNormal,
    EndTrace, StartTrace, true, vect(0,0,0),,
    TRACEFLAG_Bullet);
    DrawDebugLine(StartTrace, EndTrace, 100,100,100,);


A trace draws a direct line from one point to another. In this situation we have our
trace call our weapon's InstanteFireStartTrace() function, which in turn
calls GetPhysicalFireStartLoc(), which looks like the following code snippet:
/*****************************************************************
* Location that projectiles will spawn from. Works for secondary
* fire on third person mesh
*****************************************************************/
simulated function vector GetPhysicalFireStartLoc(optional vector
AimDir)
{
Local SkeletalMeshComponent AttachedMesh;
local vector SocketLocation;
Local TutorialPawn TutPawn;

TutPawn = TutorialPawn(Owner);
AttachedMesh = TutPawn.CurrentWeaponAttachment.Mesh;
AttachedMesh.GetSocketWorldLocationAndRotation
(MuzzleFlashSocket, SocketLocation);

return SocketLocation;
}

Quite simply, we're using our weapon's SocketLocation to start the trace. Our
trace end uses the StartTrace value and adds our weapon's MaxRange, then
multiplies it by our player controller's rotation. The HitActor value keeps track
of anything our trace hits along the way.
Free download pdf