Skip to content

Instantly share code, notes, and snippets.

@todorok1
Created April 13, 2018 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todorok1/f0414db199ada4b455366a858bb8244e to your computer and use it in GitHub Desktop.
Save todorok1/f0414db199ada4b455366a858bb8244e to your computer and use it in GitHub Desktop.
Unityチュートリアル・ボールの飛距離を計算するスクリプト。
void CheckDistance(){
if (!isCheckingDistance){
// 距離測定中でなければ何もしない
return;
}
if (rb.IsSleeping()){
// スリープモードに入ったことを検知したら距離を出力
stopPosition = gameObject.transform.position;
float distance = GetDistanceInXZ(initPosition, stopPosition);
// コンソールに表示
Debug.Log("飛距離は " + distance.ToString("F2") + " メートルです。");
// 距離測定中フラグをオフに
isCheckingDistance = false;
}
}
float GetDistanceInXZ(Vector3 startPos, Vector3 stopPos){
// 開始位置、停止位置のそれぞれで、Y軸成分を除いて計算用Vector3を作成
Vector3 startPosCalc = new Vector3(startPos.x, 0f, startPos.z);
Vector3 stopPosCalc = new Vector3(stopPos.x, 0f, stopPos.z);
// 2つのVector3データから距離を算出
float distance = Vector3.Distance(startPosCalc, stopPosCalc);
return distance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment