Related to this question How to project a Vector3 onto the xz plane?

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;

This is apparently wrong since heading stays the same regardless of where grenadePos and tmPosition are in the scene where as the grenadePos + grenadeVelocity differs for each position in the scene.
How can I do this correctly so that I can predict if indeed that grenade is coming at my teammate? My initial thought was to add grenadePos to the heading but that makes no sense as heading = teammatePos - grenadePos.

  • 1
    \$\begingroup\$ Is this going to be a one-time check at the time of launch, or a continuous check until the grenade explodes? \$\endgroup\$ Mar 14, 2023 at 7:50
  • 1
    \$\begingroup\$ Also, do you want to check against where your teammate is, or where your teammate will be? \$\endgroup\$ Mar 14, 2023 at 8:30
  • \$\begingroup\$ Remember that I'd asked you to include a diagram to indicate how you want this function to work. Right now it seems like you're not taking into account the arc of the grenade — so you don't care where the grenade is predicted to land, only the direction it's flying, is that right? \$\endgroup\$
    – DMGregory
    Mar 14, 2023 at 11:30
  • \$\begingroup\$ One way to solve this issue is to use the dot product of the normalized direction vector and the velocity vector of the grenade instead of using the dot product of the heading vector and the sum of grenade position and velocity vectors. \$\endgroup\$
    – fatdrogen
    Mar 16, 2023 at 10:43


You must log in to answer this question.

Browse other questions tagged .