The custom HUD, LinkGun and source code

 

The HUD class (CustomHUD.uc)

Our hud is the same as the one here. Just make sure you check the link if you need more explanations, so I will just give you the code with the difference that the HUD class extends from UTHUD and that a DrawHUD is already in the base class so if you want to add drawing routines like the mouse cursor, you can do so by declaring a new function.

var bool bDrawTraces; //Hold exec console function switch to display debug of trace lines & Paths.

var const Texture2D CursorTexture;

exec function ToggleIsometricDebug()
{
        bDrawTraces = !bDrawTraces;
        if(bDrawTraces)
        {
                `Log("Showing debug line trace for mouse");
        }
        else
        {
                `Log("Disabling debug line trace for mouse");
        }
}

function DrawTraceDebugRays()
{
        local CustomPlayerController PlayerController;
        PlayerController = CustomPlayerController(PlayerOwner);

        //Draw Trace from the camera to the world using
        Draw3DLine(PlayerController.StartTrace, PlayerController.EndTrace, MakeColor(255,128,128,255));
}

function vector2D GetMouseCoordinates()
{
        local Vector2D mousePos;
        local UIInteraction UIController;
        local GameUISceneClient GameSceneClient;

        UIController  = PlayerOwner.GetUIController();

        if ( UIController != None)
        {
                GameSceneClient = UIController.SceneClient;
                if ( GameSceneClient != None )
                {
                        mousePos.X = GameSceneClient.MousePosition.X;
                        mousePos.Y = GameSceneClient.MousePosition.Y;
                }
        }

        return mousePos;
}

event PostRender()
{
        local CustomCamera PlayerCam;
        local CustomPlayerController PlayerController;

        super.PostRender();

        //Get a type casted reference to our custom player controller.
        PlayerController = CustomPlayerController(PlayerOwner);

        //Get the mouse coordinates from the GameUISceneClient
        PlayerController.PlayerMouse = GetMouseCoordinates();
        //Deproject the 2d mouse coordinate into 3d world. Store the MousePosWorldLocation and normal (direction).
        Canvas.DeProject(PlayerController.PlayerMouse, PlayerController.MousePosWorldLocation, PlayerController.MousePosWorldNormal);

        //Get a type casted reference to our custom camera.
        PlayerCam = CustomCamera(PlayerController.PlayerCamera);

        //Calculate a trace from Player camera + 100 up(z) in direction of deprojected MousePosWorldNormal (the direction of the mouse).
        //-----------------
        //Set the ray direction as the mouseWorldnormal
        PlayerController.RayDir = PlayerController.MousePosWorldNormal;
        //Start the trace at the player camera (isometric) + 100 unit z and a little offset in front of the camera (direction *10)
        PlayerController.StartTrace = (PlayerCam.ViewTarget.POV.Location + vect(0,0,100)) + PlayerController.RayDir * 10;
        //End this ray at start + the direction multiplied by given distance (5000 unit is far enough generally)
        PlayerController.EndTrace = PlayerController.StartTrace + PlayerController.RayDir * 5000;

        //Trace MouseHitWorldLocation each frame to world location (here you can get from the trace the actors that are hit by the trace, for the sake of this
        //simple tutorial, we do noting with the result, but if you would filter clicks only on terrain, or if the player clicks on an npc, you would want to inspect
        //the object hit in the StartFire function
        PlayerController.TraceActor = Trace(PlayerController.MouseHitWorldLocation, PlayerController.MouseHitWorldNormal, PlayerController.EndTrace, PlayerController.StartTrace, true);

        //Calculate the pawn eye location for debug ray and for checking obstacles on click.
        PlayerController.PawnEyeLocation = Pawn(PlayerOwner.ViewTarget).Location + Pawn(PlayerOwner.ViewTarget).EyeHeight * vect(0,0,1);

        //Your basic draw hud routine
        DrawHUD();

		//CustomCursorDrawing
		DrawCustomCursor();

	if(bDrawTraces)
		DrawTraceDebugRays();
}

function DrawCustomCursor()
{
	local vector2D mouseCoords;
	mouseCoords = GetMouseCoordinates();
	Canvas.SetPos(mouseCoords.X, mouseCoords.Y);
	Canvas.SetDrawColor(255,255,255,255);
	Canvas.DrawTile(CursorTexture, 32, 32, 0, 0, 128, 128);
}

DefaultProperties
{
	//Use a target for mouse cursor.
	CursorTexture=Texture2D'EditorMaterials.TargetIcon'
}

Pretty self explanatory here :) so we move on to the new link gun and then we will be able to compile and execute. It might be a good thing to check if your code compile without major errors, it should say that you are missing the link gun class specified in the Game Info class.

The LinkGun class (CustomLinkGun.uc)

This file is rather long and was modified by Yorg Kuijs directly so you could always ask him in the forum, his nickname is DuckSauce from the source files :)

 

Download the source for the entire tutorial

 

 

Update me when site is updated
Share and Enjoy:
  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • Turn this article into a PDF!
  • Twitter
  1. Don
    April 16th, 2010 at 22:36 | #1

    Hi, just tried your source, and removed the linkgun. Compliled without errors but my pawn always start at freecamera. Any thoughts?
    Thanks!

  2. Roychr
    April 17th, 2010 at 11:35 | #2

    Make sure you specified the camera class, and that the pawn request’s the default camera you expect, like in this case top down view. The pawn has a default function for requesting a default camera mode, also in the controller you have to override some function for the pawn to correctly set the requested camera, I think its the resetCameraMode.

    Try to add Debug output to see if those function gets called.

  3. Don
    April 19th, 2010 at 03:03 | #3

    I forgot to update my INI file. DefaultGame var was wrong. DOH!
    Nice blog, I’ll continue to follow your tutorials.
    Thanks again Roy.

  4. Don
    April 25th, 2010 at 14:54 | #4

    Hi Roy, I’m doing a Top-Down shooter but I’m having problems with pawn rotation. The trace function that returns “MouseHitWorldLocation” value is not what I need. I have tall static meshes and the “newRotation” var returns the hit position, but I only need the angle/positon to rotate the pawn (to the 2D cursor angle)
    Take a look: http://img707.imageshack.us/img707/4403/udktarget.jpg

    Can you please help me? Thanks!

  5. Roychr
    April 26th, 2010 at 17:28 | #5

    Did you remove the +100 to height from the ray casted from above the pawn model ? if so your mouse cursor should deproject correctly to the scene. To correctly define the rotation values, you will take the destination point (mouse deproject) and subtract it to current pawn direction.

    There is in fact two rays that are drawned in debug mode using this tutorial, one is casted from above and start at pawn height + 100, and there is the one that start at the pawn eye level and is the same logic as when an AI tries to see if it has line of fire with an enemy, this seems to be the one in your picture as I recognize my aqua color :)

    Hope this helps and keep me posted from future troubles !

  6. Don
    April 27th, 2010 at 10:50 | #6

    Thanks for the help! But yeah, I’ve removed the +100 to vect(0,0,0). My cursor is fine, but the pawn doesnt respect the Z axis when hitlocation hit tall static meshes. I uploaded 2 videos to youtube so you can see my problem.
    without debug: http://www.youtube.com/watch?v=nqTrp2vhzec
    with debug: http://www.youtube.com/watch?v=Vb252ZVe9Eo

    I tried to force Z axis but didnt work. Oh and its your debug indeed :P
    If I change the wall static meshes collision to COLLIDE_BlockAllButWeapons, everything works fine (but the shot pass thru walls)
    I need to fix this. I dont need to test hitlocation, just want the 3D coord from 2D cursor to get the right pawn angle. Thoughts?
    Thanks again!

  7. Don
    May 3rd, 2010 at 16:28 | #7

    Hey Roy, found the solution. Its simplier then thought.

    I converted the 2D cursor positions to a angle (0 to 65536) and updated the Yaw. Worked perfect, rotating the pawn and ignoring all actors hitPosition. ;)

  8. Dn2
    August 12th, 2010 at 20:07 | #8

    using the may udk and im have problems..

    the link gun code gives errors but i dont need it so i just removed it.

    but also i cant turn the pawn with the mouse or see the cursor. i dont knoe if im doing soming wrong or its a prob with the code/may build.

  9. Dn2
    August 12th, 2010 at 23:38 | #9

    @Dn2
    its working in the Dec build after i omitted a couple of lines, but dying makes the 3rdperson pawn mesh disappear, and the red flash see at death stays on screen.

    http://dl.dropbox.com/u/9033730/Movie.wmv

  1. No trackbacks yet.