# How to align a hint arrow to point along a path?

I am using CalculatePath and trying to use path.corners in order to give user a hint for path, that where should he lead next. I am using first two instances of Vector3 of path.corners. Lets call those vectors A and B. Let me illustrate this using figure.

Now I want to position my arrow above the midpoint of the line from B to A, and orient it so it's pointing along the direction from B to A.

When I orient the arrow with LookAt(), it doesn't point in the same direction as the path. Here is an image of my arrow prefab, showing how it's oriented:

• The direction from B to A is the vector (A - B); is that what you're looking for? Jul 9, 2016 at 22:23
• I tried that but results were inaccurate, or may be I don't know how to use them correctly. For example if we talk about x component of both vectors, their difference was 64-61 = 3. This value is wrong to be plotted as coordinate on my terrain Jul 9, 2016 at 22:29
• It looks like you're trying to edit a quaternion as though it were a set of Euler angles.... this is a bit of a mess. Why are you modifying the rotation after LookAt? Jul 9, 2016 at 22:34
• Because my arrow orientation is not correct on instantiation. I mean should already rotated as I made in arrow prefab, but it is not. So I have to in order to rotate it + make it point towards correct direction Jul 9, 2016 at 22:58
• What you're doing there is not mathematically meaningful. If you add an image of your prefab with the local transform gizmo shown so I can see how it's oriented, I can show you the correct compensating rotation to apply. In general: never modify rotation.xyz — it's not the same as the Euler angles you see in the inspector (and even there, componentwise adding is dicey) Jul 9, 2016 at 23:01

It looks like your trouble isn't in computing the direction - it's that your arrow asset was imported with an unusual orientation, so when you use methods like LookAt or LookRotation they give you the "right" result for a standard orientation, leaving your arrow visually pointing somewhere else.

You tried to correct for this by adding angular offsets to the rotation, but the transform.rotation property is not measured in angles - it's a Quaternion, and so we need to use Quaternion methods if we want to modify it.

Usually the cleanest fix here is to re-export your asset in the orientation you really want, or parent it to an empty game object and then rotate the child to align properly with the parent - using the new parent as the root of your prefab.

I'll show you how we can correct the orientation in code though:

void TransformArrow(Vector3 start, Vector3 end, float height)
{
// This is similar to what you already have.
Vector3 arrowPosition = (start + end)/2f;
arrowPosition.y = height;
arrow.transform.position = arrowPosition;

Vector3 direction = (end - start);

// If you want to prevent the arrow from tilting up or down on slopes,
// uncomment this line.
// direction.y = 0f;

Quaternion arrowOrientation = Quaternion.LookRotation(direction);

// Your prefab arrow points along -x ("left"), with +z ("forward") pointing up.
// We want to rotate this to Unity's standard: +z forward, y+ up.
// We'll construct the orientation facing "left" with "forward" up, then invert it.

Quaternion correction = Quaternion.Inverse(
Quaternion.LookRotation(Vector3.left, Vector3.forward)
);

// Now we apply our orientation and the correction for the prefab's orientation.
arrow.transform.rotation = arrowOrientation * correction;
}

• Hi @DMGregory thanks for such a detailed answer and your help. I tried your code, it didn't work unfortunately :( Can you please check the chat there? I have posted comments there. Thanks Jul 10, 2016 at 22:01
• Whoops, I accidentally wrote "back" where I should have said "left" - fixed above. Jul 10, 2016 at 22:14
• Thanks for quick reply. I am not on my pc currently. Will check at night time after getting back to home. Jul 11, 2016 at 4:34
• OMG it is working :D Thank you so much. You rock DMGregory :) Jul 11, 2016 at 18:53