Calculating Direction and Distance

Hi There,

I have been going crazy trying to resolve this issue. I don’t come from a background of programming, but have managed to get myself quite the distance through unreal’s blueprints. However, I am facing one problem which is quite frustrating…

I have a character and a horse in my game, and I have setup a ‘call horse’ function so when the player calls the horse, it comes too him. Part of the code takes into account distance… ie: when the horse is called, if the horse is 5,000 or > units away from the character, the system destroys the horse actor, and recreates the horse at 4,000 units away, at which point the horse goes to the character. So far this works perfectly fine, but where I am facing a problem is when the horse is destroyed, and recreated at the 4000 unit mark, I cannot figure out how to get him to recreate in the ‘same relative direction (to the player)’ from where he was destroyed. So basically now I have him get recreated for example, at ‘x of the player’ - 4000; ‘y of the player - 4000’. This always creates the horse from a single direction relative to the character before he starts running towards the character.

Instead, what I am looking for is to have him horse created 4000 units away from the player, but in the same relative direction that the destroyed horse was at. I would really appreciate it if someone could advise me on how I can get that done, because it is driving me crazy :stuck_out_tongue:



Some simple vector math will help:

  1. calculate vector from player to horse. ie “horse location” minus “player location”
  2. normalize that vector, now you have direction vector to horse
  3. multiply that direction vector by 4000 (that is distance you want horse to spawn)
  4. add that vector to player location, now this is location for horse to spawn.

If your map is bumpy (big differences in Z axis), you may in step 1, nullify Z values in both location. (multiply both locations by 1,1,0)
Then for resulting horse spawn spot you do line trace and find Z value for map at that spot.