参考になったサイト
検証用コード
- unityのHierachyViewに3つのオブジェクトを作成して確かめた
using UnityEngine;
using System.Collections;
public class TestDotProduct : MonoBehaviour {
public Transform vec1;
public Transform vec2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Debug.Log("vec1.position "+vec1.position);
Debug.Log("vec2.position "+vec2.position);
Debug.Log("transform.position "+transform.position);
Vector3 OVec1 = vec1.position - transform.position;
Vector3 OVec2 = vec2.position - transform.position;
Debug.Log("OVec1 "+OVec1);
Debug.Log("OVec2 "+OVec2);
Debug.Log("OVec1.magnitude "+OVec1.magnitude);
Debug.Log("OVec2.magnitude "+OVec2.magnitude);
float dotFunction = Vector3.Dot(OVec1,OVec2);
float dot = OVec1.x * OVec2.x + OVec1.y * OVec2.y + OVec1.z * OVec2.z;
Debug.Log("dotFunction "+dotFunction);
Debug.Log("dot "+dot);
float cosTheta = dotFunction / (OVec1.magnitude * OVec2.magnitude);
Debug.Log("cosTheta "+cosTheta);
float radian = Mathf.Acos(cosTheta);
Debug.Log("radian "+radian);
float degree = radian * 180 / Mathf.PI;
Debug.Log("degree "+degree);
Debug.Log("");
Debug.Log("");
}
}
車の前方方向の速度を正確に取得するには以下の2つのURLを読む
法線/垂直ベクトルの計算