How to give line trace a range limit


I need a line trace to go from the enemy towards the player, but end at a distance from the enemy. The reason is that I want to check if the player is within close combat range. I guess that the trick is to do some sort of math equation to get the end slot of the line trace to act in this way, but my math skills are weak - I can’t figure it out.

Has anyone done something like this or have an idea of how to do it?


Um why do a trace? VectorA - VectorB and then get the vector length wich is basically the distance how far 2 vectors are away from ech other

ruohki is right, it doesnt really make sense to do a line trace, maybe use a sphereComponent and an overlap event for the specific actor your looking for?

Both those ideas would work - but would I not lose the ability to make sure that there was not wall or object between the player and the character? I want to make sure that the enemy don’t punch me through thin walls/ each other and so on.

Well then a line trace would be “perfect”? just do a line trace from the enemies BP, start at its actor location then get the actor forward vector and add as many units as you want the trace to be.

Edit: This is from content examples -> physics(map) which shows a line trace that starts at a actor location and ends at the length of the “Interaction Distance” float.

In case anyone actually needs to have a line trace stop some distance before the target. Check this out: