# How to display distance between two actors on screen when clicking on one of them...?

**URL:** <https://forums.unrealengine.com/t/how-to-display-distance-between-two-actors-on-screen-when-clicking-on-one-of-them/341581>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [December 16, 2015, 6:06am UTC](https://forums.unrealengine.com/t/how-to-display-distance-between-two-actors-on-screen-when-clicking-on-one-of-them/341581 "2015-12-16T06:06:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AMRKC](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AMRKC](https://forums.unrealengine.com/u/AMRKC)\
**Post date:** [December 16, 2015, 6:06am UTC](https://forums.unrealengine.com/t/how-to-display-distance-between-two-actors-on-screen-when-clicking-on-one-of-them/341581/1 "2015-12-16T06:06:30Z")

</div>

I was creating a c++ unreal project. i had an instant where i have to display the distance between two actors on game screen. It is first person game. I have created a HUD with cross hair in the center. There are several static mesh actors placed on the ground. During game play it must display the distance of that actor from another reference object, when i click over it. What should i do. Please help…

---

<div class="post-metadata">

**Author:** ![vipeout](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@vipeout](https://forums.unrealengine.com/u/vipeout)\
**Post date:** [December 16, 2015, 8:11am UTC](https://forums.unrealengine.com/t/how-to-display-distance-between-two-actors-on-screen-when-clicking-on-one-of-them/341581/2 "2015-12-16T08:11:01Z")

</div>

Trace from camera position, calculate forward vector based on camera rotation, multiply by the range you want the items to count. Having something as a return value from the trace, get ‘HitActor’ after breaking the result struct trace provides.

Then take that HitActor location, calculate the distance to the second object you need, then either ‘PrintString’ it onto the screen or make a widget, add to the viewport and use TextField ‘SetText’ function to set the actual text that it should display (there are several nice float to text formatting nodes).

The widget itself will be simply a TextBlock put anywhere you wish on the screen. In the PlayerController on BeginPlay just create it, store the reference and call ‘AddToViewport’. You can show/hide it when needed.

I’ve recently made an example trace from camera right ahead in front of it here:

> **[Activating an actor with the E key](https://forums.unrealengine.com/questions/344515/activating-an-actor-with-the-e-key.html)**
>
> I’m trying to create an actor that will do something when the E key is pressed, the player is looking directly at the actor and they are within a certain distance. how do I accomplish this using blueprints? the perspective of the game is 3rd person...

---

<div class="post-metadata">

**Author:** ![AMRKC](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AMRKC](https://forums.unrealengine.com/u/AMRKC)\
**Post date:** [December 16, 2015, 8:42am UTC](https://forums.unrealengine.com/t/how-to-display-distance-between-two-actors-on-screen-when-clicking-on-one-of-them/341581/3 "2015-12-16T08:42:56Z")

</div>

I want the distance displayed when i click over that…what should i do…can u suggest a sample code…plzzzzz…

---

<div class="post-metadata">

**Author:** ![AMRKC](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AMRKC](https://forums.unrealengine.com/u/AMRKC)\
**Post date:** [December 16, 2015, 8:44am UTC](https://forums.unrealengine.com/t/how-to-display-distance-between-two-actors-on-screen-when-clicking-on-one-of-them/341581/4 "2015-12-16T08:44:58Z")

</div>

Or just tell me how to display a message when i click over a static mesh c++ actor…

---

<div class="post-metadata">

**Author:** ![vipeout](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@vipeout](https://forums.unrealengine.com/u/vipeout)\
**Post date:** [December 16, 2015, 9:54am UTC](https://forums.unrealengine.com/t/how-to-display-distance-between-two-actors-on-screen-when-clicking-on-one-of-them/341581/5 "2015-12-16T09:54:45Z")

</div>

I am not a programmer. I cannot give you a sample code. I can do that easily in blueprints and I told you how. If you want the distance displayed in-editor and that is what you mean by ‘click’ then I cannot help you since I have no idea how to add widgets to editor viewport or whether it is even possible.

I believe however, that you meant in-game, and that is what I described. What I said would be the way to do it in blueprints. Again, I cannot help you with code, I did quite some C++, but never within the UE4.

---

<div class="post-metadata">

**Author:** ![AMRKC](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AMRKC](https://forums.unrealengine.com/u/AMRKC)\
**Post date:** [January 12, 2016, 5:49am UTC](https://forums.unrealengine.com/t/how-to-display-distance-between-two-actors-on-screen-when-clicking-on-one-of-them/341581/6 "2016-01-12T05:49:02Z")

</div>

Thank U…
