UnrealScript Game Programming Cookbook

(Chris Devlin) #1

Miscellaneous Recipes


228

We then draw a debug line so that we can see the trace. The line takes our start trace
and end trace as parameters. You don't need this line, but it certainly makes your job
far easier.


  1. Let's go over to the next step, that is, converting 3D coordinates into a 2D space.
    /* Projection for the crosshair to convert 3d coords
    into 2d
    /
    ScreenPos = Canvas.Project(HitLocation);
    /* If we haven't hit any actors... /
    if (HitActor == None )
    {
    HitActor = (HitActor == None)? None
    : Pawn(HitActor.Base);
    HitLocation = EndTrace;
    ScreenPos = Canvas.Project(HitLocation);
    }


ScreenPos, or the 2D vector we want to draw the crosshair on, is using our Canvas.
Project function and takes HitLocation as a parameter. Project is used when
you want to take a 3D coordinate and draw it on a 2D space, like our HUD. The opposite
function, Deproject, takes a 2D coordinate and converts it to 3D space.
Our HitLocation parameter changes depending on how we set that value. Our
HitLocation parameter is the actor our trace has crossed. If we haven't hit any
actors, HitLocation is where the trace ends (that is, it may just extend off into
the distance on a map without walls).


  1. Now we need to draw the actual crosshair.
    /* Draws the crosshair for no one - Grey/
    CrosshairSize = 28 (Canvas.ClipY / 768)
    (Canvas.ClipX /1024);
    Canvas.SetDrawColor(100,100,128,255);


// Crosshair in center of trace
Canvas.SetPos(ScreenPos.X - (CrosshairSize * 0.5f),
ScreenPos.Y -(CrosshairSize * 0.5f));
Canvas.DrawTile(class'UTHUD'.default.AltHudTexture,
CrosshairSize, CrosshairSize, 600, 262, 28, 27);
return false;

Our crosshair size uses the edge of our screen, both the X and Y values, then divides
those values by the standard screen resolution (1020 x 768) and multiples it by 28
so that it is large enough to see. Our crosshair color can be anything we'd like, but I've
set it so that it is gray for now.
Free download pdf