# How to project a Vector3 onto the xz plane?

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;

{
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.

• 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. Mar 13, 2023 at 11:18
• But wouldn't heading be calculated with different values for tmPosition and grenadePos when they get moved to different parts of the scene? Mar 14, 2023 at 0:29
• 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. Mar 14, 2023 at 0:49
• 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. Mar 14, 2023 at 3:13
• Actually that sounds wrong, I'll post new question here. Mar 14, 2023 at 3:21

Original vector:                    (1, 2, 3)