I have this code that tries to check whether a lobbed grenade will head toward a teammate:

    var heading = tmPosition - grenadePos;
    var distance = heading.magnitude;
    var normalizedDir = heading / distance;

    if (Vector3.Dot(heading, (grenadePos + grenadeVelocity)) < 1.05f && Vector3.Dot(heading, (grenadePos + grenadeVelocity)) > 0.95f)
        return true;

tmPosition is the position of my teammate. The grenade can be lobbed for long distances or thrown like a baseball (with a slightly arcing trajectory) for closer range so my teacher says to project its trajectory as well as the heading vector on to the ground xz plane to make sure that my code above that predicts whether a particular grenade is heading towards my teammate or not.

How do I do this? I saw the API for Vector3.Project and Vector3.ProjectOnPlane but the examples are not very good.

  • 2
    \$\begingroup\$ Your code is incorrect. It mixes apples and oranges. heading is an offset vector, while (grenadePos + grenadeVelocity) is an absolute position. That means if I move the pair of teammate and grenade objects to different parts of the scene, without changing the relative offset between them, I'll get different answers from this function, even though the true answer has not changed. I'd recommend asking a separate question about how to detect this case correctly, including a diagram to show which cases should return true and which should return false. \$\endgroup\$
    – DMGregory
    Commented Mar 13, 2023 at 11:18
  • \$\begingroup\$ But wouldn't heading be calculated with different values for tmPosition and grenadePos when they get moved to different parts of the scene? \$\endgroup\$ Commented Mar 14, 2023 at 0:29
  • \$\begingroup\$ It would, but since we're moving them together, the value would still be the same. "2 to the left and 3 up" is the same offset vector, whether you're measuring from (0, 0) to (-2, 3) or from (2, -3) to (0, 0) or from (100, 100) to (98, 103). But in each of those places grenadePos is different, so you're comparing one vector that's always the same against one vector that keeps changing, leading to changing answers when you want the answer to stay the same. \$\endgroup\$
    – DMGregory
    Commented Mar 14, 2023 at 0:49
  • \$\begingroup\$ Wait a second, then all I have to do is add the grenadePos to the offset vector and it would become an offset dependent on the initial grenadePos, different each time I believe. \$\endgroup\$ Commented Mar 14, 2023 at 3:13
  • \$\begingroup\$ Actually that sounds wrong, I'll post new question here. \$\endgroup\$ Commented Mar 14, 2023 at 3:21

1 Answer 1


You can 0-out the Y coordinate. This is called an orthographic projection:

Original vector:                    (1, 2, 3)
Ortographic projection on XZ plane: (1, 0, 3)

You must log in to answer this question.

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