Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to draw a crosshair at the end of my trace.

If my crosshair does not hit a pawn or static mesh (ex, just a skybox) then the crosshair stays locked on a certain point at that actor - I want to say its origin.

Ex: Run across a pawn, then it turns yellow and stays on that pawn. If it runs across the skybox, then it stays at one point on the box. Weird?

How can I get my crosshair to stay consistent? I've included two images for reference, to help illustrate.

Note: The wrench is actually my crosshair. The "X" is just a debug crosshair. Ignore that. /// Image 1 /// /// Image 2 ///

/***************************************************************************
* Draws the crosshair
***************************************************************************/
function bool CheckCrosshairOnFriendly()
{ 
local float             CrosshairSize;
local vector            HitLocation, HitNormal, StartTrace, EndTrace, ScreenPos;
local actor             HitActor;
local MyWeapon          W;
local Pawn              MyPawnOwner;

/** Sets the PawnOwner */
MyPawnOwner = Pawn(PlayerOwner.ViewTarget); 
/** Sets the Weapon */
W = MyWeapon(MyPawnOwner.Weapon);   

/** If we don't have an owner, then get out of the function */
if ( MyPawnOwner == None )
{
    return false;
}   

/** 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,);

    /** Projection for the crosshair to convert 3d coords into 2d */ 
    ScreenPos = Canvas.Project(HitLocation); 

    /** If we haven't hit any actors... */
    if ( Pawn(HitActor) == None )
    {
        HitActor = (HitActor == None) ? None : Pawn(HitActor.Base);
    }
}

/** If our trace hits a pawn... */
if ((Pawn(HitActor) == None))
{
    /** Draws the crosshair for no one - Grey*/
    CrosshairSize = 28 * (Canvas.ClipY / 768) * (Canvas.ClipX /1024);
    Canvas.SetDrawColor(100,100,128,255);
    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;
}   

    /** Draws the crosshair for friendlies - Yellow */
    CrosshairSize = 28 * (Canvas.ClipY / 768) * (Canvas.ClipX /1024);
    Canvas.SetDrawColor(255,255,128,255);
    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 true;

}

share|improve this question

1 Answer 1

up vote 1 down vote accepted

If you aren't hitting anything, your HitLocation will be vect(0,0,0). In cases like this, you just need a:

if(HitActor == none)
{
   HitLocation = EndTrace;
}
share|improve this answer
    
Bingo! Worked like a charm, thank you! –  Dave Voyles Dec 10 '12 at 15:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.