1
\$\begingroup\$

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.

Image of a path on the ground from point B to A, with an arrow hovering above, pointing in a slightly different direction.

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:

Image of arrow prefab, with local transform gizmo shown. The arrow points in the -x direction with +z facing up

\$\endgroup\$
7
  • \$\begingroup\$ The direction from B to A is the vector (A - B); is that what you're looking for? \$\endgroup\$
    – DMGregory
    Commented Jul 9, 2016 at 22:23
  • \$\begingroup\$ 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 \$\endgroup\$
    – NightFury
    Commented Jul 9, 2016 at 22:29
  • \$\begingroup\$ 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? \$\endgroup\$
    – DMGregory
    Commented Jul 9, 2016 at 22:34
  • \$\begingroup\$ 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 \$\endgroup\$
    – NightFury
    Commented Jul 9, 2016 at 22:58
  • \$\begingroup\$ 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) \$\endgroup\$
    – DMGregory
    Commented Jul 9, 2016 at 23:01

1 Answer 1

3
\$\begingroup\$

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;
}
\$\endgroup\$
4
  • \$\begingroup\$ 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 \$\endgroup\$
    – NightFury
    Commented Jul 10, 2016 at 22:01
  • \$\begingroup\$ Whoops, I accidentally wrote "back" where I should have said "left" - fixed above. \$\endgroup\$
    – DMGregory
    Commented Jul 10, 2016 at 22:14
  • \$\begingroup\$ Thanks for quick reply. I am not on my pc currently. Will check at night time after getting back to home. \$\endgroup\$
    – NightFury
    Commented Jul 11, 2016 at 4:34
  • \$\begingroup\$ OMG it is working :D Thank you so much. You rock DMGregory :) \$\endgroup\$
    – NightFury
    Commented Jul 11, 2016 at 18:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .