您好我正在尝试使用unity和csharp为我的游戏创建一个ai 我不知道该如何去做我想让ai向4个方向移动,所以如果玩家是对角线,那么它会向下移动然后离开等等
这是我尝试过的,但只有当我移动并且它向左或向右倾斜时才会移动
void OnTriggerStay2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
if (transform.position.y < other.gameObject.transform.position.y)
{
rigidbody2D.transform.position += Vector3.up * speed * Time.deltaTime;
anim.SetBool ("Backwards", true);
}
if (transform.position.y > other.gameObject.transform.position.y)
{
rigidbody2D.transform.position += Vector3.down * speed * Time.deltaTime;
anim.SetBool ("Front", true);
}
if (transform.position.x > other.gameObject.transform.position.x)
{
rigidbody2D.transform.position += Vector3.left * speed * Time.deltaTime;
anim.SetBool ("Left", true);
}
if (transform.position.x > other.gameObject.transform.position.x)
{
rigidbody2D.transform.position += Vector3.right * speed * Time.deltaTime;
anim.SetBool ("RIght", true);
}
}
}