URL: http://forum.unity3d.com/threads/33215-Vector-rotation
ある障害物に対しては避けたいことをしたかった。
このサイトを参考にした。
ターゲットに対して進むコードは。
Vector3 targetVec = targetTransform.localPosition - myTransform.localPosition; myTransform.position += targetVec.normalized * speed;
落とし穴などを避けたいときに、Holeタグのものに対して直角に動かしたい、
そのHoleを避けるフラグisAvoidHoleをOn/Off切り替えた
void OnTriggerEnter (Collider other) { GameObject go = other.gameObject; if (go.tag == "Hole") { isAvoidHole = true; Debug.Log("OnTriggerEnter"); } } void OnTriggerExit (Collider other) { GameObject go = other.gameObject; if (go.tag == "Hole") { isAvoidHole = false; Debug.Log("OnTriggerExit"); } }
よって結果こうなった
Vector3 targetVec = targetTransform.localPosition - myTransform.localPosition; targetVec.y = 0; if (isAvoidHole) { Debug.Log("avoid"); Vector3 vec = targetVec.normalized; Quaternion quat = Quaternion.AngleAxis(90, vec); myTransform.position += (quat * Vector3.up) * speed; } else { Debug.Log("chase"); myTransform.position += targetVec.normalized * speed; }