- Home /
How to calculate speed & distance on a sphere?
Hello,
So I am doing this game where the characters runs forever on a sphere Object. I want to calculate how manny feets & miles he travels. I found out that 1 foot equals 0.17 degrees on the eurlangles.x of the sphere.
I tried :
float rota = transform.eulerAngles.x;
Debug.Log(rota);
if(rota % 0.17<=0.1f)
{
feet++;
}
It kind of works, the problem is that it's on Update and it's doing it when it's not supposed to, or too often.
I am looking for a way to count 1 feet each time the sphere turns 0.17 degrees.
Any ideas?
Answer by UniqueMAX · Aug 08, 2015 at 06:53 PM
The problem probably occurs because you calculate distance based on rotation and not the actual distance traveled by sphere. What if sphere jumps for a frame or two? It would mess numbers. I'd use this
private float Dist=0f;
private Vector3 PrevPosition;
void Start(){
StartPosition=transform.Position;
}
void Update(){
Dist+=Vector3.Distance(transform.Position,PrevPosition);
PrevPosition=transform.Position
}
Now this way you would have exact amount of distance travelled by your sphere. Another idea to make it your way is to use FixedUpdate or LateUpdate instead of Update.