0
\$\begingroup\$

How to get distance between two player in a photon multiplayer game. Condition is both the player is moving. I have tried something like this, in my player control script:

if (photonView.isMine)
pos1 = rigidBody.position;
else
pos2 = rigidBody.position;
Distance= Vector3.Distance(pos1-pos2)

But its not working. For a player the pos2 is always (0,0). Please help.

\$\endgroup\$
  • \$\begingroup\$ First of all, there is no such overload for Vector3.Distance that takes just one Vector3, or maybe you wrote it quickly for the sake of example, you could also try (pos1-pos2).magnitude, which returns the same. Second, we need more context of your problem to know the solution, from what I can see there is that both, whatever the case, pos1 and pos2 have the same position assignated, so in no way your distance will be above 0.0f. \$\endgroup\$ – LinkWindcrafter Mar 20 '18 at 18:34
  • \$\begingroup\$ exactly. So how can I get to know the distance? Lets say Player1 's distance from player2 is increasing or decreasing ? \$\endgroup\$ – Arijit Mar 20 '18 at 18:53
  • \$\begingroup\$ for starters, that both pos1 and pos2 must be on different positions to have a distance different that 0. To know if a distance is increasing or decreasing, is just a matter of a delta of distances, you should have a priorDistance variable and an actualDistance variable, and compare them. \$\endgroup\$ – LinkWindcrafter Mar 20 '18 at 19:11
  • \$\begingroup\$ Yes I am trying exactly like that. I used the above code inside my update () function. \$\endgroup\$ – Arijit Mar 21 '18 at 14:05
  • \$\begingroup\$ But again, that part of the algorithm is correct, but the assignations for both pos1 and pos2 are incorrect, because whatever the case, both will have rigidBody.position, therefore there will be no distance. I suggest you to make the class so each one has its respective photon, then compare the distance of each class' photon. \$\endgroup\$ – LinkWindcrafter Mar 21 '18 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.