从起点到终点的距离,以更新百分比表示
问题描述:
float curpos = player.transform.position.magnitude;
float targetpos=GameObject.Find ("target").transform.position.magnitude;
float percentDist = (curCarpos / targetpos) * 100;
我想做的是计算从当前点一直到终点的百分比,从零开始,一旦到达终点就应该是100.
What I want to do is calculate percentage along the way from current point which is always moving to end point, starting from zero and once it reaches it should be 100.
由于某种原因,它从14%下降到0%,然后从0%-99%开始.
For some reason it is starting from 14% decreasing to 0% and then starting from 0%-99%.
答
您做的所有事情都是错误的.向量大小在这种情况下是没有用的.您需要这样的东西:
You do it all wrong. Vectors magnitude in this case it is useless. You need something like this:
在进行所有动作之前先打电话给它.
Call this before all movement.
float startDistance = Vector3.Distance(player.transform.position, target.transform.position);
这是运动中的.
float currentDistance = Vector3.Distance(player.transform.position, target.transform.position);
float percentage =(currentDistance / startDistance) * 100f;