老师,我在做拾荒者的控制敌人移动课程时,出现了敌人全部移动到左下角的问题,下面是问题截图,然后下面是Enemy的代码
private Vector2 targetPosition;
private Transform player;
private Rigidbody2D rigidbody;
public float smoothing = 3;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
rigidbody = GetComponent<Rigidbody2D>();
targetPosition = transform.position;
GameM.Instance.enemylist.Add(this);
}
private void Update()
{
rigidbody.MovePosition(Vector2.Lerp(transform.position, targetPosition, smoothing * Time.deltaTime));
}
public void Move()
{
Vector2 offset = player.position - transform.position;
if (offset.magnitude < 1.1f)
{
//攻击
}
else
{
float x = 0;
float y = 0;
if (Mathf.Abs(offset.y) > Mathf.Abs(offset.x))
{
//向Y轴移动
if (offset.y < 0)
{
y = -1;
}
else {
y = 1;
}
}
else
{
if (offset.x > 0)
{
x = 1;
}
else {
x = -1;
}
}
targetPosition += new Vector2(x,y);
}