У меня есть враг и персонаж. По Видеоролику сделал его движение по x, но по y не получилось, помогите сделать движение по y.
Вот мой код:
private Rigidbody2D phys;
public Transform player;
public float speed;
public int DistanceToAgr;
void Start()
{
phys = GetComponent<Rigidbody2D>();
}
void Update()
{
float distToPlayer = Vector2.Distance(transform.position, player.position);
if (distToPlayer < DistanceToAgr)
{
if (player.position.x < transform.position.x) //лево
{
phys.velocity = new Vector2(-speed, 0);
}
else if (player.position.x > transform.position.x) //право
{
phys.velocity = new Vector2(speed, 0);
}
//Пробовал по y, но не получилось
//else if (player.position.y < transform.position.y)
//{
//phys.velocity = new Vector2(0, -speed);
//}
}
else
{
phys.velocity = new Vector2(0, 0);
}
}