# playMaker

### Author Topic: Action for: Get Direction and Distance Between Two Objects?  (Read 1409 times)

#### tcmeric

• Hero Member
• Posts: 654
##### Action for: Get Direction and Distance Between Two Objects?
« on: May 24, 2017, 10:22:39 AM »
Do we already have an action to get the direction and distance between two objects (without raycasting) ? I couldnt see one.

https://docs.unity3d.com/Manual/DirectionDistanceFromOneObjectToAnother.html

If not, I will write one because I need it. But if we have one, better not to write another. Thanks!

#### djaydino

• Hero Member
• Posts: 3503
##### Re: Action for: Get Direction and Distance Between Two Objects?
« Reply #1 on: May 24, 2017, 10:41:53 PM »
Hi,
For the 'distance' i think "get distance2" (on the Ecosystem) is what you are looking for.

and for the direction maybe "Get Angle To Target" (standard action) , "Transform Direction Advanced" (Ecosystem) , "Get Delta Angle" , "Get Signed Angle To Target"

#### tcmeric

• Hero Member
• Posts: 654
##### Re: Action for: Get Direction and Distance Between Two Objects?
« Reply #2 on: May 25, 2017, 02:18:32 AM »
Hi, thanks for the help! In the end, none were quite right to get the direction between two game objects in a vector 3 (with possible normalization), aka heading.

I ended up writing my own action. Attached.

This action calculates the direction between two gameobjects (source and target) and gives a vector 3 direction. If you choose normalization, it also calculates the distance (magnitude) and normalizes the direction to give a "heading". This could be used to steer/point one gameobject towards another. The normalization has a separate toggle because it take an extra calculation to do it. Therefore if you don't need it, untoggle it.

You may not need the distance/ un-normalized direction if you are using the normalize direction (heading), but I gave the variable outputs anyways, since they are already being calculated (you might as well get access to them, in case you want to use them elsewhere).
« Last Edit: May 25, 2017, 02:20:10 AM by tcmeric »

#### djaydino

• Hero Member
• Posts: 3503
##### Re: Action for: Get Direction and Distance Between Two Objects?
« Reply #3 on: May 25, 2017, 05:50:12 AM »
Hi,
Maybe it is a good idea to also add a possibility to add  Vector 3 Values instead of gameobjects, like in some other actions (like in "Get Distance2")

#### Mulbin

• Junior Playmaker
• Posts: 82
##### Re: Action for: Get Direction and Distance Between Two Objects?
« Reply #4 on: May 28, 2017, 04:21:42 AM »
I ended up writing my own action. Attached.

I'm just getting...

error CS0234: The type or namespace name `AI' does not exist in the namespace `UnityEngine'. Are you missing an assembly reference?

What am I doing wrong?

#### tcmeric

• Hero Member
• Posts: 654
##### Re: Action for: Get Direction and Distance Between Two Objects?
« Reply #5 on: May 28, 2017, 07:39:14 AM »
It was using unity.ai . Its unnecessary, so I removed that line. But not sure why your project wouldnt be able to find it.

Here is the action without that line.

#### Mulbin

• Junior Playmaker
• Posts: 82
##### Re: Action for: Get Direction and Distance Between Two Objects?
« Reply #6 on: May 28, 2017, 07:42:04 AM »
Great thanks!  After a bit of googling it may be because I'm still using unity 5.4/  Sadly updating completely destroys large parts of my game so after several attempts I've decided to stick with 5.4