The Infinity Blade Forums Have Moved

We've launched brand new Infinity Blade forums with improved features and revamped layout. We've also included a complete archive of the previous posts. Come check out the new Infinity Blade forums.
See more
See less

[Code] Speedometer, analog and digital using Canvas

  • Filter
  • Time
  • Show
Clear All
new posts

    [Code] Speedometer, analog and digital using Canvas

    ey up

    heres something that ive been asked about many times, 'how did you make that speedo on the hud?'
    this one will give a digital speed readout, in approximate MPH.
    also when you are driving a vehicle an analog speedo will appear, the speed needle will rotate to indicate speed.
    i have used materials found in udk so you dont have to download anything, thats why it look a bit **** feel free to make your own materials.
    the red square is the background image, the white line is the needle that rotates.

    /* */
    class Speedo_HUD extends MobileHUD;
    var MaterialInstanceConstant Needle; //material for the speed needle
    var MaterialInstanceConstant BackGround; //speedo background material
    var Pawn P;  //owners pawn
    var Vehicle V;  //owners vehicle
    const MPHConversionFactor = 0.042612;   // speed conversion (rough mph)
    var int RotationFactor;  //how much the needle rotates
    simulated event PostBeginPlay()
    	PlayerOwner = PlayerController(Owner); //ref to the player controller
    function DrawHUD()
       //speed conversion vars
       local int MPH;
       local float ForwardSpeed;
       local rotator Rot;
       //reference to the owner pawn and vehicle
       P = PlayerOwner.Pawn;
       V = Vehicle(P);
       Canvas.Font = class'Engine'.Static.GetLargeFont();   //set the font
       Canvas.SetDrawColor(255,0,0);   //set the colour R,G,B
       //check if a pawn exists
       if (P != None)
          //work out forward speed
          ForwardSpeed = (P.Velocity << P.Rotation).X;
           MPH = ForwardSpeed * MPHConversionFactor;
          //so we dont get negative mph
          if (MPH <= 0)
              MPH -=MPH;
          DrawNumbers(MPH);   //draw the speed in numbers
          //check if players in a vehicle
          if (V != None)
            Rot.Yaw = MPH * RotationFactor;  //rotation for speed needle
            DrawSpeedo(Rot);  //draw analog speedo cos we in a vehicle
    //gets called if the player has a pawn
    function DrawNumbers(int MPH)
       Canvas.SetPos(100,12);  //set the position
       Canvas.DrawText("MPH " $ MPH); 
    //gets called if the player has a vehicle
    function DrawSpeedo(rotator Rot)
       Canvas.SetPos(50,100); //set the position
       Canvas.DrawMaterialTile(BackGround, 128, 128, 0, 1.0, 1.0);  //background
       Canvas.SetPos(50,150); //set the position
       Canvas.DrawRotatedMaterialTile(Needle, Rot, 128, 8, 0, 0, 1.0, 1.0); // needle
       bShowOverlays=true //possibly doesnt need to be here for this
    and for anyone who needs it, a gametype that uses this hud.
    /* */
    class Speedo_Game extends UTGame;
      HUDType=class'Speedo_HUD' //use the speedo hud for this gametype
      bUseClassicHUD=true  //turns off UT scaleform hud
    now go see if you can break the sound barrier woo!

    i have tried get this working for mobile so i have changed UTGame to SimpleGame but when i tested in the mobile viewer the analog input seems to be gone any idea why ? and how to fix the problem ?
    never mind i solved the problem


      ThX lad, works like a charm.


      EDIT: Actually I didnt get it working in mobile previewer, please how did you patch that up, lost?

      EDIT2: Solved.

      EDIT3: Also if anyone wants to use It with texture on hud and so just change the subclass from MobileHUD to UDKHUD.