0. 界面操作设置
1. 设置一个左点和右点, 根据检测list中识别到的物体,切换当前target,向target移动
transform.position = Vector2.MoveTowards(transform.position, targetPoint.position, speed * Time.deltaTime);
2. 判断那边远向哪边
if (Mathf.Abs(pointA.position.x - transform.position.x) > Mathf.Abs(pointB.position.x - transform.position.x))
{
targetPoint = pointA;
}
else
{
targetPoint = pointB;
}
3. 使用rotation或者scale进行翻转, 同时翻转组件
if (transform.position.x < targetPoint.position.x)
{
transform.rotation = Quaternion.Euler(0f, 0f, 0f);
}
else
{
transform.rotation = Quaternion.Euler(0f, 180f, 0f);
}
4. 距离边界,一般取0.01f
if (Mathf.Abs(transform.position.x - targetPoint.position.x) < 0.01f)
{
SwitchPoint();
}
5. 添加trriger的list
private void OnTriggerStay2D(Collider2D other)
{
if (!attackList.Contains(other.transform))
{
attackList.Add(other.transform);
}
}
private void OnTriggerExit2D(Collider2D other)
{
attackList.Remove(other.transform);
}