You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.7 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pterodactyl : MonoBehaviour
{
const float horizontalSpeed = 300f;
const float verticalSpeed = 200f;
DragonMovement[] _dragons;
// Start is called before the first frame update
void Start()
{
_dragons = FindObjectsOfType<DragonMovement>();
}
// Update is called once per frame
void Update()
{
DragonMovement closestPlayerToMyLeft = null;
float closestDistance = float.MaxValue;
foreach (DragonMovement dragon in _dragons)
{
float distance = Vector3.Distance(gameObject.transform.position, dragon.transform.transform.position);
if (distance < closestDistance && dragon.transform.position.x < this.transform.position.x)
{
closestDistance = distance;
closestPlayerToMyLeft = dragon;
}
}
float verticalDirection = 1f;
if (closestPlayerToMyLeft != null && closestPlayerToMyLeft.transform.position.y < this.transform.position.y)
verticalDirection = -1f;
if (closestPlayerToMyLeft == null)
verticalDirection = 0;
transform.position = new Vector3(transform.position.x - Time.deltaTime * horizontalSpeed, transform.position.y + (Time.deltaTime * verticalSpeed * verticalDirection), transform.position.z);
if (transform.position.x < -100f)
{
GameObject.Destroy(this.gameObject);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Fireball"))
{
GameObject.Destroy(this.gameObject);
}
}
}