# [Unity实战]判断角色面朝一定区域是否存在物体

2015-03-30

```using UnityEngine;
using System.Collections;

public class TestRot : MonoBehaviour {

public Transform target;

void Start()
{
//四元数rotation的一些运算
Quaternion rotation = target.rotation * Quaternion.Euler(0f, 30f, 0f);//当前角度绕y轴旋转30度
Vector3 pos = rotation * new Vector3(10f, 0f, 0f);//当前角度沿x轴前进10个单位

Debug.DrawLine(pos, Vector3.zero, Color.red);
Debug.Log("newpos " + pos + " nowpos " + target.position + " distance " + Vector3.Distance(pos, target.position));

//向量旋转
//当前向量绕y轴旋转45度，也可认为现将角度绕y轴旋转45度，在沿z轴前进1个单位
Vector3 a = Quaternion.Euler(0f, 45f, 0f) * Vector3.forward;//只旋转，向量的长度是不变的
Debug.Log(a);
}
}```

```using UnityEngine;
using System.Collections;

public class TestRot2 : MonoBehaviour {

public Transform target;

public float angle = 60f;
public float distance = 5f;

void Update()
{
Vector3 direction = target.position - transform.position;

if (Vector3.Angle(direction, transform.forward) < angle)
if (Vector3.Distance(target.position, transform.position) < distance)
print("我已经锁定你了！");
}
}
```