Unityで、移動するオブジェクトの方向を、キャラクターが見るスクリプトをLookAtを用いて作ってみた。
1)移動するターゲットオブジェクトを追加し、inspectorのAdd Componentで以下のスクリプト(movingTarget)を追加する。

using UnityEngine;
using System.Collections;
public class movingTarget : MonoBehaviour {
bool movingPlus = true; // プラス方向に移動中か?
Vector3 initial_position; // オブジェクトの初期位置
float randRange = 3f; // 乱数の範囲
float posRange = 5f; // オブジェクトの移動範囲
float speed = 3f; // オブジェクトの移動速度
bool xAxis = true; // x軸方向か?
// Use this for initialization
void Start()
{
// キャラクターの頭部位置取得
Transform headObj = GameObject.Find("SuitMan").transform;
// オブジェクトの移動軸の選定
if (Random.Range(-1f, 1f) > 0)
xAxis = true;
else
xAxis = false;
// オブジェクトの位置を、頭部位置+ランダムに設定
Vector3 rand = new Vector3(Random.Range(-randRange, randRange), 0, Random.Range(-randRange, randRange));
transform.position = headObj.position + rand;
initial_position = transform.position;
}
// Update is called once per frame
void Update()
{
if (movingPlus)
{
if (xAxis)
{
transform.position += new Vector3(speed * Time.deltaTime, 0f, 0f);
if ((transform.position.x - initial_position.x) >= posRange)
movingPlus = false;
}
else
{
transform.position += new Vector3(0f, 0f, speed * Time.deltaTime);
if ((transform.position.z - initial_position.z) >= posRange)
movingPlus = false;
}
}
else
{
if (xAxis)
{
transform.position -= new Vector3(speed * Time.deltaTime, 0f, 0f);
if ((transform.position.x - initial_position.x) <= -posRange)
movingPlus = true;
}
else
{
transform.position -= new Vector3(0f, 0f, speed * Time.deltaTime);
if ((transform.position.z - initial_position.z) <= -posRange)
movingPlus = true;
}
}
}
}
このスクリプトでは、キャラクターSuitManの位置を中心にランダムに、ターゲットオブジェクトの位置を設定し、ランダムに選択したx軸またはy軸の方向に移動するものである。
2)キャラクターSuitManに、以下のスクリプト(LookAt)追加する

using UnityEngine;
using System.Collections;
public class LookAt : MonoBehaviour {
Transform targetObj; // ターゲットオブジェクト
// Use this for initialization
void Start () {
// ターゲットオブジェクトの取得
targetObj = GameObject.Find("Target").transform;
}
// Update is called once per frame
void Update () {
// 体をターゲットオブジェクトに向ける
transform.LookAt(targetObj);
}
}
体ごと回転するので不自然さはあるが、これでキャラクターを移動するターゲットオブジェクト方向を向かせることができる。
なお、自然な動きをさせるためには、FinalIKのLookAtIKを使った方がよい。
https://www.assetstore.unity3d.com/jp/#!/content/14290
Look At IK