添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

As the title said, I use Vector3.Distance to get two objects’ distance, something like enemy chase player, and I found that even the enemy “catch” the player, the two object is close to each other, I mean they’re all in same position, overlaping each other, but the value this api return is not what I thought, the value is keeping in around 0 for a little while, then it start to became larger and larger. I wonder if has some reason to explain that, it’s weird.

I use NavMeshAgent to set “player.transform.position” as destination.

public override void Reason(GameObject player, GameObject npc)
    NPCControl npcControl = npc.GetComponent<NPCControl>();
    float distance = Vector3.Distance(npc.transform.position, player.transform.position);
    Vector3 Ppos = player.transform.position;
    Debug.Log("P(" + Ppos.x + ", " + Ppos.y + ", " + Ppos.z + ")");
    Vector3 Epos = npc.transform.position;
    Debug.LogWarning("E(" + Epos.x + ", " + Epos.y + ", " + Epos.z + ")");
    float Dst = Vector3.Distance(Ppos, Epos);
    Debug.LogError("P-E(" + Dst + ")");

The debug log showing below:

WayneJP:

As the title said, I use Vector3.Distance to get two objects’ distance, something like enemy chase player, and I found that even the enemy “catch” the player, the two object is close to each other, I mean they’re all in same position, overlaping each other, but the value this api return is not what I thought, the value is keeping in around 0 for a little while, then it start to became larger and larger. I wonder if has some reason to explain that, it’s weird.

I use NavMeshAgent to set “player.transform.position” as destination.

public override void Reason(GameObject player, GameObject npc)
    NPCControl npcControl = npc.GetComponent<NPCControl>();
    float distance = Vector3.Distance(npc.transform.position, player.transform.position);
    Vector3 Ppos = player.transform.position;
    Debug.Log("P(" + Ppos.x + ", " + Ppos.y + ", " + Ppos.z + ")");
    Vector3 Epos = npc.transform.position;
    Debug.LogWarning("E(" + Epos.x + ", " + Epos.y + ", " + Epos.z + ")");
    float Dst = Vector3.Distance(Ppos, Epos);
    Debug.LogError("P-E(" + Dst + ")");

What is the value you get?

Is it by chance something like:
1.9878904432890e-36

It’s scientific notation, the e-36 (or whatever negative number after the e) is saying move the decimal point that many digits left.

So really that value would be like:
0.000000000000000000000000000000000001987890443289

1.9878904432890e-36

It’s scientific notation, the e-36 (or whatever negative number after the e) is saying move the decimal point that many digits left.

So really that value would be like:
0.000000000000000000000000000000000001987890443289

This is the debug log show:

After all, I set y equals zero to fix this problem.

doctorpangloss:

Your characters are animating, or the physics colliders are pushing them apart. It’s probably animations of root motion. This won’t be the case with two spheres with no colliders, you should test that to understand that clearly Vector3.Distance isn’t wrong.

I change the part of code like following, the reason why ths problem happen is still need to dig deeper.

Vector3 from = new Vector3(npc.transform.position.x, 0f, npc.transform.position.z);
    Vector3 to = new Vector3(player.transform.position.x, 0f, player.transform.position.z);
    float distance = Vector3.Distance(from, to);

Not sure what the jump upto 12.2 and back down to 3.6 is. Not sure your setup honestly… do they have animations playing on them? Do they have colliders bumping into one another? What controls their movement? Where is the origin/center of each GameObject relative to colliders/mesh/etc?

There’s so many possibilities for those values behaving like that.

Like imagine you have 2 GameObject with capsule colliders and a mesh. But the mesh and capsule collider of one is configure so the feet of the mesh is at the origin, but the other the center of the mesh/collider are at the origin. This means when they’re close to one another, there’s going to be added distance in the y direction, since their origins are offset in the y-direction.

The Distance function does exactly what it should. In your console logs you can see that P and E are not close to each other. While they are very close on x, and within a couple units of each other on z, they increae or decrease their relative position on y by a lot, which is then represented in their total distance.

What’s interresting tho, is that in your code you always print a Log, a Warning and an Error in that order - however this is not reflected in the console logs you showed us. Are you, by any chance, doing these calculations on some thread or something like that, which could cause them to be shuffled like this?